AWS S3–7 Useful CLI Commands
AWS S3–7 Useful CLI Commands
www.awstutor.io

AWS CLI

AWS S3–7 Useful CLI Commands

S3 offers a simple API, unlimited storage and can work seamlessly with other AWS applications. Here are 7 useful S3 CLI commands.

Manish Manalath
Nov 11 · 4 min read

Amazon Simple Storage Service (Amazon S3) is an object-based storage service that you can use to store and protect any amount of data for a range of use cases, such as websites, mobile applications, backup, etc. In this article, we will go through a few useful CLI commands that you can use to work with S3.

If you don't have an AWS account or the CLI installed, this post will help you. If you are stuck during installation, let us know in the comments.

For those of you who are new to S3, S3 stores data in containers called “Buckets”. S3 bucket names are globally unique, which means that after a bucket is created, the name of that bucket cannot be used by another AWS account until the bucket is deleted.

Let's look at a few S3 commands.

CREATE BUCKET

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 should receive the message ‘make_bucket:<bucket-name>’.

LIST BUCKETS

Now that you have created a bucket, let's list the available buckets in your S3 account. 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.

UPLOAD FILE

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 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.

$ 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.

DOWNLOAD FILE

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.

$ 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>

DELETE FILE

Let's delete a file. We use the remove (rm) command to delete a 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 ‘delete:s3://bucket-name/<filename>’.

DELETE BUCKET

To delete an entire bucket, use the remove bucket (rb) command. Make sure the bucket is empty or AWS will refuse to delete the bucket.

$aws s3 rb s3://bucket-name

If successful, you should see the message ‘remove_bucket:bucket-name’

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:

Hope this article helped you get familiar with S3 and the AWS CLI in general. If you have any questions, let us know in the comments.


Don’t forget to clap if you liked this article. Signup for our weekly newsletter and we will send you a summary of articles every week. No spam. No ads.

AWS Tutor

Your daily dose of Amazon Web Services.

Manish Manalath

Written by

AWS Professional. Helping people understand AWS, one article at a time. http://awstutor.io

AWS Tutor

AWS Tutor

Your daily dose of Amazon Web Services.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade