How to rename S3 objects
In this tutorial, we will use the AWS CLI to rename objects inside an S3 bucket.
We will create an S3 bucket, upload some files from the local computer, and rename objects using the AWS CLI.
Prerequisites
To complete this tutorial you need:
- An AWS Identity Center user account with privileges to create S3 buckets.
- AWS CLI installed and configured on your computer.
AWS S3 buckets and objects
A bucket in AWS S3 is a container for objects. The objects are the files stored inside a bucket. You can manage your buckets and objects via the AWS GUI console. But for some operations like renaming objects, you must use the AWS CLI.
There are two S3 CLI commands.
-
aws s3api
command gives you the access to to lower level parameters for manipulating S3 buckets and objects. -
aws s3
is an easy-to-use command for managing buckets and objects similar to how you manage files and folders in the Linux shell.
For this tutorial, we will use aws s3
command.
Create bucket and upload objects
Open the terminal in the computer where the AWS CLI is installed and login to AWS portal via the CLI.
aws sso login --sso-session <session-name>
If you run into problems refer to this tutorial on how to install and configure AWS CLI.
Create a new bucket named cloudqubes-bucket-1
.
aws s3 mb s3://cloudqubes-bucket-1 --profile <profile-name>
Use the correct profile name according to your .aws/config
file.
An S3 bucket name must be globally unique because an S3 bucket can be accessed via its URL. If you try to create a bucket with a name that’s already in use, you will get
BucketAlreadyExists
error.
In a local working path create the following files using the touch
command.
mkdir text
mkdir images
touch config.text
touch text/file-1.text
touch text/file-2.text
touch text/file-3.text
touch images/image-1.png
touch images/image-2.png
touch images/image-3.png
Once created, the file hierarchy will look like this.
- config.text
- image
|- image-1.png
|- image-2.png
|- image-3.png
- text
|- file-1.text
|- file-2.text
|- file-3.text
Sync the current path with S3 bucket.
aws s3 sync ./ s3://cloudqubes-bucket-1 --profile <profile-name>
The sync
command accepts two parameters; the source and the destination.
List the bucket contents.
$ aws s3 ls s3://cloudqubes-bucket-1 --recursive --profile <profile-name>
2024-03-14 04:14:52 0 config.text
2024-03-14 04:14:53 0 images/image-1.png
2024-03-14 04:14:53 0 images/image-2.png
2024-03-14 04:14:53 0 images/image-3.png
2024-03-14 04:14:53 0 text/file-1.text
2024-03-14 04:14:53 0 text/file-2.text
2024-03-14 04:14:53 0 text/file-3.text
AWS S3 does not have a concept like folders in the Linux file system. All S3 objects are stored in a flat hierarchy.
But for keeping the objects organized, S3 implements the concept of folders by grouping objects with a prefix
. In the above object listing, text/
is a prefix that groups the three objects file-1.text
, file-2.text
, and file-3.text
.
Rename objects
We use the command aws mv
to rename.
Rename one object
Renmae config.text
file to conf.text
.
aws s3 mv s3://cloudqubes-bucket-1/config.text s3://cloudqubes-bucket-1/conf.text --profile <profile-name>
Rename multiple objects
Add a prefix config
to all files in text/
path.
$ aws s3 mv s3://cloudqubes-bucket-1/text/ s3://cloudqubes-bucket-1/text/config --recursive --profile <profile-name>
move: s3://cloudqubes-bucket-1/text/file-1.text to s3://cloudqubes-bucket-1/text/config/file-1.text
move: s3://cloudqubes-bucket-1/text/file-3.text to s3://cloudqubes-bucket-1/text/config/file-3.text
move: s3://cloudqubes-bucket-1/text/file-2.text to s3://cloudqubes-bucket-1/text/config/file-2.text
For renaming multiple files the parameter --recursive
is required.
Wrapping up
The aws s3
CLI has several commands for manipulating S3 buckets and objects.
Type in aws s3 help
in the CLI to check out the documentation for other commands.