Simple Storage Service (S3) is one of the core services on the AWS platform. In this guide, you’ll learn how to use the AWS S3 command line interface (CLI) tool to manage your files.
What is the Amazon Web Services S3 CLI?
The AWS CLI tool allows you to manage many of AWS’s services using a command. The S3 command offers the ability to:
- Copy files
- List files/bucket contents
- Move files
- Sync files
- Create new S3 buckets
- And much more!
You can learn more about the available S3 commands here. You can also create a script that uses the AWS S3 CLI tool to programmatically create buckets on the fly, move files around, and keep local and remote files in sync.
What is AWS S3?
AWS S3 stands for Simple Storage Service (the 3 S’s). S3 is one of AWS’s storage services, others being Glacier, Elastic Block Storage, and Elastic File System.
S3 provides a service known as “object storage,” which allows you to easily manage objects anywhere from the world using the CLI tool, web interface, or API. S3 offers 99.999999999% durability and many features such as backup and recovery. You can learn more about S3 in the official S3 documentation.
How to Copy Files with AWS S3 CLI
One of the most useful features of the AWS S3 CLI tool is the ability to copy files. Let’s first create a test file on our local system:
echo "Hello World" > test.txt
We have just created a text file named “test.txt” with contents of “Hello World”. To copy our file onto S3 let’s first create a new bucket:
aws s3 mb s3://change-me-123
The S3 namespace is globally shared by all S3 users and you will often find that common names have already been used by someone else to create a bucket. To verify that the bucket was created:
aws s3 ls
This will list all of the buckets in your S3 account with the creation date and time. Let’s now move our
test.txt file into our newly created bucket:
aws s3 cp test.txt s3://change-me-123/
This should return a value that looks like:
upload: ./test.txt to s3://change-me-123/test.txt
Our test file has been successfully uploaded into our S3 bucket. We can also verify this by listing the contents of our
change-me-123 bucket again:
aws s3 ls s3://change-me-123/
AWS S3 CLI Documentation
The AWS S3 CLI documentation is bundled within the general CLI documentation. This will give you an overview of the CLI tool, but you can dig deeper into specific S3 documentation here.
Within the S3 CLI documentation, you will find a list of commands, arguments, path requirements and some examples. A command will often follow this structure:
aws s3 [COMMAND] [ARGUMENT]
aws s3 mb s3://test-bucket
This example makes a new bucket named ‘test-bucket’ using the
mb command which then takes
s3://test-bucket as the argument value. If you are ever stuck or need a quick reference then the CLI tool has some useful built in documentation. If you want to learn more about a specific command, use the
help argument. There are three levels of help hierarchy:
aws helpcommand will give you a list of available options / commands.
aws s3 helpcommand will give you a list of available options / commands related to that service, in this case ‘s3’.
aws s3 mb helpcommand will give you a list of available options / commands related to the ‘mb’ command within the ‘s3’ service.
The AWS S3 CLI provides a relatively simple way for you to control the S3 service from the command line. This allows you to manage your storage easily, and even to automate it using scripts or applications. With this knowledge, you’re ready to start managing objects more easily than ever before!