Neeraj Rathod
Neeraj Rathod

Reputation: 1655

How to move all files and folder from one folder to another of S3 bucket in php with some short way?

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

Upvotes: 33

Views: 72119

Answers (4)

Chuck
Chuck

Reputation: 11

There is a way kinda brute force, but works if you can do it in either gitbash or on a mac terminal

for x in `aws s3 ls s3://<bucket> | grep <prefix> | awk '{print $4}'`
do
   aws s3 mv s3://<bucket>/$x s3://<bucket>/folder/$x
done

This will get the list from aws, get just your expected files and the awk gives you just the file names and the for loop will issue the move for each file. It is slow but gets the job done.

Upvotes: 1

GStav
GStav

Reputation: 1166

I think you should try using the command line API:

aws s3 mv s3://bucket1/key/to/folder/ s3://bucket2/key/to/folder2 --recursive

Upvotes: 21

MattJHoughton
MattJHoughton

Reputation: 1118

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

Upvotes: 22

Michael - sqlbot
Michael - sqlbot

Reputation: 179084

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.

Upvotes: 49

Related Questions