How do you rename a folder in a bucket on S3?
As simple as it sounds, it seems like an extraordin开发者_JAVA百科arily complicated task.
If you're using the aws-s3 gem, the following code will rename folder OLD_FOLDER_NAME in bucket BUCKET_NAME to NEW_FOLDER_NAME:
bsize = OLD_FOLDER_NAME.size
bucket = AWS::S3::Bucket.find(BUCKET_NAME)
bucket.objects({:prefix=>OLD_FOLDER_NAME}).each do |o|
AWS::S3::S3Object.rename(o.key, NEW_FOLDER_NAME + o.key[bsize..-1], BUCKET_NAME)
end
That's it. Folders aren't real objects, so all you have to do is rename all the objects that end up in that specific path to the new path. The virtual folder will be renamed as a result.
Seems like the AWS Command Line Interface is the new way to do stuff like this. Using it, you can rename a folder like this:
aws s3 mv --recursive s3://bucketname/oldfoldername s3://bucketname/newfoldername
Be sure and start with the --dryrun
option to make sure it will do what you think it's going to do.
If you use the S3 Management Console, you can cut and paste. Go into the folder you want, click Ctrl + A, then click Actions > Cut. Make your new folder and click Actions > Paste.
Use BucketExplorer! This is a great app! You can do pretty much anything you ever wanted to do to your s3 in a very very easy to understand GUI
Go into the S3 console and use the following:
aws s3 --recursive mv s3://<bucketname>/<folder_name_from> s3://<bucket>/<folder_name_to>
精彩评论