31

I have to move all files and folders from one folder or another of S3 bucket in php. I know a way to do the same thing is - 1) get all objects list from the source folder 2) copy all objects into destination folder 3) delete all objects from source folder

Is there any other short way to do the same. If so then please share with me, It would be appreciated, Thanks in advance

Neeraj Rathod
  • 1,609
  • 4
  • 18
  • 25

3 Answers3

46

Is there any other short way to do the same

There is no short way to do this.

Here is the reason:

The concept of folders is unique to the console. Amazon S3 uses buckets and objects, but the service does not natively support folders, nor does it provide any API to work with folders.

To help you organize your data, however, the Amazon S3 console supports the concept of folders. [...]

Important

In Amazon S3, you create buckets and store objects. The service does not support any hierarchy that you see in a typical file system.

The console uses the object key names to derive the folder hierarchy. It uses the "/" character in the key name to infer hierarchy

http://docs.aws.amazon.com/AmazonS3/latest/UG/about-using-console.html

"Moving" files to another "folder," in S3, cannot be done, in reality. It can only be emulated, by making copies of each individual object, giving each object a new key name to the new "hierarchy," then deleting the original. S3 does not even support renaming an individual object. Renaming is also accomplished by making a copy with the new name, then removing the original. The console gives the appearance of supporting these operations, but it actually only emulates them, as described above. This is a deliberate part of the design of S3.

Michael - sqlbot
  • 169,571
  • 25
  • 353
  • 427
  • 2
    Thank you Michael for making it so easily understandable. :) – Neeraj Rathod Oct 06 '16 at 10:43
  • 2
    An example in PHP is available here: https://stackoverflow.com/questions/21184720/how-to-rename-files-and-folder-in-amazon-s3#25471915. And an example for the AWS CLI is available here: https://stackoverflow.com/questions/21184720/how-to-rename-files-and-folder-in-amazon-s3#35099142. – rapture Mar 21 '19 at 16:06
  • The answer above using the web client made moving objects from one folder to another dead simple – Jesse Novotny Jun 01 '22 at 18:51
16

Welcome, from the future!

Amazon has now provided this feature.

Select your bucket, and next to the file or 'folder' (still flat structured) you want to move check the checkbox.

Now hit the actions dropdown, and then click "move".

enter image description here

MattJHoughton
  • 1,040
  • 13
  • 19
15

I think you should try using the command line API:

aws s3 mv s3://bucket1/key/to/folder/ s3://bucket2/key/to/folder2 --recursive
GStav
  • 1,066
  • 12
  • 20
  • Thank you! I also wanted to mention that adding "--dryrun" may be helpful in order to make sure it's going to do what you expect it to do. – umberto-petrov Mar 24 '23 at 01:05
  • I wanted to move all objects at the root(/) to a folder named "permanent". In this case, should I use `--recursive` flag ? --recursive is used to move folders right ? – AdiL IsmaiL Aug 08 '23 at 19:06
  • Yes, --recursive is for moving subdirectories too. – GStav Aug 09 '23 at 08:29