Search code examples
pythonamazon-web-servicesamazon-s3boto3file-rename

How to remove spaces in filenames on s3 bucket programmatically using boto with python?


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


Solution

  • 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().