AWS S3–7 CLI Commands That Will Make Your Life Easier
S3 offers a simple API, unlimited storage and can work seamlessly with other AWS applications. Here are 7 useful S3 CLI commands.
Amazon Simple Storage Service (Amazon S3) is a storage service that you can use to store all types of data from simple documents to gigabytes of data archives. AWS S3 can be used for a range of use cases such as hosting websites, storing data for mobile applications, big data analytics, etc.
One popular use case of S3 is that you can use it to host websites and small web applications for free. I have recently written an article on how you can host a react web application on S3.
S3 uses a structure similar to directories called ‘Buckets’. An S3 bucket is a storage container with a globally unique name, making it easy to generate download links if you want to share your data with others.
AWS CLI is a command-line tool that you can use to interact with most of the AWS services. With the CLI, developers can quickly execute commands on their terminal that perform the required actions on the AWS platform. If you don’t have an AWS account or the CLI installed, this post will help you.
In this article, we will go through a few useful CLI commands that you can use while working with S3.
Since Buckets are the starting point in S3, let's create a new bucket. You have to make sure the name is available or you will S3 won't create the bucket for you.
To create a new bucket, use the mb command along with the bucket name.
$ aws s3 mb s3://bucket-name
If the bucket name is not available, you will receive the following error.
If the bucket is created successfully, you will receive the message:
Now that you have created a bucket, let’s list the available buckets in your S3 account. You can use the ls command to list all buckets.
$ aws s3 ls
The AWS CLI will list the bucket you just created. If you don’t see your bucket listed, go into your AWS console and make sure your bucket is created.
Now let’s upload a file to our bucket. You can use two commands for uploading files, the copy (cp) and the move(mv) commands.
The copy command copies your file to the S3 bucket while retaining the original in your system. The move command, however, removes the local copy and sends it to your S3 bucket.
To copy a file:
$ aws s3 cp <filename> s3://bucket-name
To move a file:
$ aws s3 mv <filename> s3://bucket-name
If the commands are successful, you should receive a message saying:
copy/move : <filename> to s3://bucket-name/<filename>
LIST BUCKET CONTENTS
To list the contents of a single bucket, we will use the same ls command. However, we will specify the bucket name so that S3 knows we are looking for the contents of a single bucket and not the list of all buckets in your AWS account.
$ aws s3 ls s3://bucket-name
You should get a list of the files in your bucket along with the size and last updated time.
There is no point in uploading a file to the cloud if you cant fetch it when you need it. You can use the same cp command to download a file from S3 to your computer.
$ aws s3 cp s3://bucket-name/file <filename>
If the file exists in the bucket and downloaded successfully, you should receive the message:
download: s3://bucket-name/<filename> to <filename>
Let’s delete a file. Use the remove (rm) command along with the file path to delete the file from an S3 bucket.
$ aws s3 rm s3://bucket-name/<filename>
If the file exists and is deleted successfully, you should receive the message:
To delete an entire bucket, use the remove bucket (rb) command. Make sure the bucket is empty or AWS will not delete the bucket.
$aws s3 rb s3://bucket-name
If successful, you should see the message:
BONUS COMMAND (HACK)
Its a bit hard to remember all these commands. Even though the AWS CLI offers a manual page, there is a quick way to check the available commands.
If a command issued is not available, AWS CLI will list all available commands. So if you try
$ aws s3 commands
you should get the following message:
AWS S3 is a great tool for your data storage, data protection, and data archival requirements. The CLI is designed to help you interact with the platform more efficiently. Also, the AWS SDK is just a wrapper for the AWS CLI that lets you execute AWS operations using a programming language.
Hope this article helped you get familiar with S3 and the AWS CLI in general. If you have any questions, let me know in the comments. You can also signup for my newsletter to receive a summary of articles once a week.