Exercise: Upload a file to Amazon S3 using AWS CLI

Natasha Ong
This is some text inside of a div block.
4 min read

Exercise Overview:

In this hands-on exercise, you will use the AWS CLI to access Amazon Simple Storage Service to access S3.

By the end of this hands-on exercise, you should be able to do the following:

  • Create an S3 bucket using AWS CLI
  • Upload an object using AWS CLI
  • Delete the S3 bucket using AWS CLI

Task 0: Accessing the AWS Management Console

  1. Sign in to your IAM user and access your AWS Management Console.
  2. Select your preferred region for doing this exercise. We recommend picking the region that's closest to you.

Task 1: Create an S3 bucket with mb command

In this task, you will create an S3 bucket using one of the commands in CLI which is the mb command (mb = make bucket).

1. Open your terminal (for macOS/Linux OS) or Windows PowerShell (for Windows).

2. Access your AWS resources through AWS CLI. Need a refresher? Have a little peek at Task 3 in our previous exercise about installing AWS CLI.

3. Once you successfully complete step 2 in this task, let's create a bucket.

Type the following command:

The following mb command creates a bucket. In this example, the user makes the bucket mybucket. Replace RANDOM_NUMBER with a random number so that it would make your bucket unique. By default, if you do not specify a Region in the command, the Region will be the same as the one you used when entering your access key ID and secret access key in step 2.

BONUS: If you're curious, here's how you would specify a region (us-west-1):

4. Once you finish the mb command, the expected output would be:


Task 2: Upload a file from the local computer to the S3 bucket

In this task, you will upload a file from your local computer to the S3 bucket that you've created.

1. Choose any file from your local computer, e.g. an image or text file.

To upload the file from your local computer, the command would look like this: aws s3 cp "<location_of_file>" s3://<bucket_name>

For example:

  • aws: Invokes the AWS CLI.
  • s3:  The command is for operations related to Amazon S3
  • cp: Copy.
  • "C:\Users\Username\OneDrive\Documents\SampleFile.txt": The file path on the local Windows machine. Here, a document "SampleFile.txt" is inside the Documents folder inside the user's OneDrive. Your file path will look different*.
  • s3://mybucketRANDOM_NUMBER: Defines the destination, which is an S3 bucket named mybucket0248429. The s3:// prefix is used to specify that the path is in the S3 bucket namespace.

*Don't know how to find your file's file path?

  • MacOS: Find your file inside Finder, and press Command + Option + P. A file path bar should pop up at the bottom of your window! Right click on your file, and click on "Copy x as Pathname."
  • WindowsOS: Locate your file where you saved it. Right-click and select Copy as path.

2. You should see an output similar to the below one after you successfully run the command.


3. To verify if the uploaded file is in the S3 bucket, type the command:

  • ls: ls = list. It's a command used to list the contents (e.g. files and subfolders) in an Amazon S3 bucket.
  • s3://mybucketRANDOM_NUMBER: The path to your S3 bucket.
  • --recursive: when you use this option, the command (in this case, the ls command) is performed on all files or object in the s3 bucket. Without --recursive, the command only lists the contents of the root of the bucket. Since our file is stored in the root of the bucket anyway, --recursive doesn't change our output too much - but it's good practise and might be helpful for you in the future!

4. The output would look similar to this:



Task 3: Delete the S3 resources

In this task, you will delete the S3 object and the S3 bucket. You will use the following commands:

  • rm: Deletes an S3 object.
  • rb: Deletes an empty S3 bucket. A bucket must be completely empty of objects before it can be deleted.

1. To delete the S3 object, type the command:


2. You should see an output like this:

3. To delete the S3 bucket, type the command:


4. You should see an output like this:


BONUS: If the bucket still has objects inside, add  --force to first remove all of the objects in the bucket. The bucket will automatically remove itself afterwards. Type the command:


Optional: Verify if the object and bucket have been deleted:

S3 object: You should see an error that says "NoSuchBucket" after running this command

S3 bucket: This command tries to lists all the buckets you have, and in this case you have none! So you should expect nothing to come up:

Congratulations! You have successfully done the following:

  • Created an S3 bucket using the AWS CLI command
  • Uploaded an object using the AWS CLI command
  • Removed the S3 bucket and S3 object using the AWS CLI command