-3

so I have 1000s of files on my s3 bucket. But, the names of the files contain spaces. I want to replace the space with '_' programmatically using boto python. How do I achieve this?

Thanks.

Snapshot of filenames: enter image description here

sachin kumar s
  • 99
  • 3
  • 12
  • 1
    What did you try so far? – assli100 Mar 09 '21 at 09:37
  • I didnt execute anything, I tried to lookup on different approaches of getting this task done. No idea so far. One post says copy to another object with a different filename. Not sure if that's efficient. – sachin kumar s Mar 09 '21 at 10:05

1 Answers1

1

It is not possible to rename objects in Amazon S3.

Instead, you would need to call CopyObject() to copy the object, and then DeleteObject() to delete the original object.

Alternatively, here's a cheating method that I use:

  • I create an Excel spreadsheet with the name of the files
  • Then, in Column B, I create a formula to convert the spaces into underlines, so it contains the desired name
  • In Column C, I write a command that renames the object, like this:
="aws s3 mv 's3://bucketname/"&A1&"' s3://bucketname/"&B1
  • Test the command by pasting it into a terminal window
  • If it works, use Copy Down to create the formula for all of the files
  • Copy that column, paste it into a .sh file, then run the file (preferably on an Amazon EC2 instance to reduce network latency)

The AWS CLI aws s3 mv command will call CopyObject() and DeleteObject().

John Rotenstein
  • 241,921
  • 22
  • 380
  • 470