Cade Holmes
Cade Holmes

Reputation: 11

Amazon S3 - How to recursively rename files?

I'm trying to fetch my files via the s3.getObject() method in my node.js backend.

Trouble is, upon uploading the files to my bucket, I failed to replace special characters, dashes, and white-spaces. So, any files that have a Key value of (e.g., a Key with the value of 10th Anniversary Party (Part 1) 1-23-04 has an endpoint of 10th+Anniversary+Party+(Part+1)+1-23-04).

This becomes troublesome when trying to encode the URI for fetching. I'd like to replace all dashes, white-space, and special chars with a simple underscore. I've seen some possible conventions using the aws-cli, however I am unsure what the best command for this is. Any advice would be greatly appreciated.

Upvotes: 1

Views: 832

Answers (1)

John Rotenstein
John Rotenstein

Reputation: 269490

You could write a program that:

  • Lists the contents of the bucket
  • Calls CopyObject() to copy the object to a new Key
  • Calls DeleteObject() to delete the previous copy

Or, you could take advantage of the fact that the AWS CLI offers a aws s3 mv command that will Copy + Delete for you.

I often simply create an Excel spreadsheet with the existing names, and a formula for determining what name I'd like. Then, I create a third column with:

aws s3 mv [Column 1] [Column 2]

Use Copy Down on the rows to get all the mv commands. Then, copy the column of commands, paste them into the command-line and it will rename all the objects in Amazon S3! (Test with 1-2 lines first, in case there is an error in the formula.)

This might seem primitive, but it's a very quick way to make the changes.

Upvotes: 1

Related Questions