Intro: Is it possible to rename files and folders in Amazon S3?
How to rename files or folders in Amazon S3 is not a piece of cake as is in traditional file systems. It gets a little trickier! Say you have uploaded several files in different prefixes (directories) to Amazon S3. And now you need to rename the files or folders. How can you do that? Yes, there is a way. Amazon S3 service consists of objects with keys called prefixes. Using “/” gives a folder feeling, but technically there are no folders or files to mention in Amazon S3 like on your regular Desktop.
So in this post, we will mention how you can still perform typical file system-like actions such as renaming files or folders in Amazon S3.
Edge around to rename files and folder in Amazon S3
There is no direct method to rename a file or folder in Amazon S3. But we can perform a little bit edge around. What you have to do is to create a new “folder” in S3 and then move all of the files from that “folder” to the new “folder.” Once all files are moved, we can remove the source “folder.”
There are multiple ways you can do this. I prefer to use the AWS CLI (Command Line Interface). We will basically rename files and folders in Amazon S3 using the command line.
How to use AWS CLI to rename files and folder in Amazon S3?
Install and configure AWS CLI
First, download and install the AWS CLI appropriate for your operating system. After installing you can check with
aws --version command using terminal or command prompt.
Next, you need to configure the AWS CLI. You are using your AWS credential information for this. You are asked to enter your AWS Access Key ID and AWS Secret Access Key during configuration. Here’s how to access them. We use the
aws configure command to start the configuration.
List the buckets in Amazon S3 using AWS CLI
After making our settings, we are ready. Let’s start by listing all our buckets under our Amazon S3 service. Use the
aws s3 ls command for this.
You can not rename a bucket for Amazon S3 because there are technically no folders in S3. So we have to handle every file within the bucket. That means you have to create a new bucket, copy the contents from the new bucket and delete the old bucket. Now, let’s just list the “folder” and files in my sample mybukcetmg bucket with
aws s3 list s3://<bucket_name>
How to rename folders in Amazon S3?
As you see above, there is a prefix (folder) named sample and a jpeg file named trial in the mybucketmg bucket. Now we rename the sample folder to sample_new. For this, we will use (see Highlight 1 below) the command
aws s3 --recursive mv s3://<bucket_name>/folder_name_old s3://<bucket_name>/folder_name_new
Above notice that sample folder is renamed while moving to the same bucket mybucketmg. Meanwhile,
document_old2.txt under the sample are automatically moved to the sample_new folder (see Highlight 2 above). We achieve this with the
--recursive flag in the command. Let’s check the latest status of mybucketmg and sample.
So far we have renamed the sample folder to sample_new. The contents of our sample folder have also been moved to the folder we have just named. Finally, let’s complete the process by deleting our old and empty sample folder with
aws s3 --recursive rm s3://<bucket_name>/<folder_name_old>/
How to rename files in Amazon S3?
Let’s rename the trial.JPG file under mybucketmg to trial_new.JPG We use the
aws s3 mv s3://<bucket_name>/<file_name_old> s3://<bucket_name>/<file_name_new> command for this.
TL,DR or Summary
For renaming folders in Amazon S3:
aws s3 --recursive mv s3://<bucketname>/<folder_name_old> s3://<bucket>/<folder_name_new>
For renaming files in Amazon S3 :
aws s3 mv s3://<bucketname>/<file_name_old> s3://<bucket>/<file_name_new>
So what methods do you use to rename files and folders in Amazon S3? Please share with us in the comments.