How to Download File From AWS S3 With AWS CLI on Ubuntu [Made Easy]

Introduction

AWS CLI is a command line tool to access your AWS services. With the help of AWS CLI, you can configure, control multiple AWS services from the command line and also automate them through scripts.

In this tutorial, you will download all files from AWS S3 using AWS CLI on a Ubuntu machine.

You can also follow the below tutorial in AWS EC2 instance to transfer files between AWS S3 and AWS EC2.

Prerequisites

Viewing Your S3 Buckets

In this section, you will view the objects in your S3 bucket using cli. 

s3 ls is the command used to list your S3 buckets. Use this command to view your S3 Buckets.

First, open your terminal. Next type the following command.

aws s3 ls
  • aws – Command to invoke AWS Client
  • S3 – Denotes the service where the operation to be performed
  • ls – Command to list your S3 Buckets.

You’ve viewed the Buckets in your AWS S3. In the next step you’ll download a single file from your S3 Bucket.

Downloading a Single File From S3 Bucket

In this step, you’ll download a single file from your S3 Bucket with AWS CLI.

You’ll use copy command cp to copy a file from S3 bucket to the the local directory. Use the following command to download a text file to your local directory

aws s3 cp s3://existing_bucket_name/file_name.txt ./destination
  • aws – Command to invoke AWS Client
  • S3 – Denotes the service where the operation to be performed
  • cp – Command to copy file between a local system and S3 Bucket
  • s3://existing_bucket_name/file_name.txt– Source location of the file
  • ./destination – Destination location of the file

To learn more about the optional parameters in the cp command, you can refer the official docs page.

You’ve downloaded a single file from your S3 bucket. In the next step, you’ll download all files from AWS S3 Bucket.

Downloading All Files From AWS S3 Bucket

In this section, you’ll download all files from AWS S3 bucket to a directory using two ways.

  • Using copy recursive
  • Using Sync

For the demonstration purposes, consider there are two files (index.html and styles.css in your S3 Bucket). Now, you’ll see how copy recursive and sync will work with two files.

Using Copy Recursive

In this step, you’ll download all files from AWS S3 Bucket using cp command to the the local directory. Use the following command to download all files from AWS S3. It might take time to download depending upon file size and internet bandwidth.

aws s3 cp s3://existing_bucket_name ./destination --recursive

You’ll see the below output which means the two files are downloaded to your local path.

download all files from AWS S3
Figure 1 – Download all files from AWS S3 Bucket

You’ve download all files from AWS S3 Bucket using cp –recursive. Next, You’ll download all files from AWS S3 Bucket using sync command.

Using Sync

Sync is a command used to synchronize source and target directories. Sync is by default recursive which means all the files and subdirectories in the source will be copied to target recursively.

Use the below command to Sync your local directory to your S3 bucket.

aws s3 sync s3://full_s3_bucket_name/ your_local_directory

You’ll see the output below.

Output

download: s3://full_s3_bucket_name/index.html to your_local_directory/index.html
download: s3://full_s3_bucket_name/styles.css to your_local_directory/styles.css

Since there are no files in your target location, both the files will be copied. If a file is already existing, then only one file will be copied.

You’ve download all files from AWS S3 Bucket using cp and Sync command.

Conclusion

You’ve viewed files in your S3 bucket and copied files from it using cp and sync command with AWS Cli. Copying files answers your question How to download all files from AWS S3 bucket. You can follow the above steps in AWS EC2 instance to transfer files between AWS S3 and AWS EC2.

If you have any questions or if you face any problem while following the tutorial, feel free to comment below.

What Next?

You can upload files into your S3 Bucket using AWS CLI . Refer the guide How to Upload Files to AWS S3 Using Command Line?.

<Watch this space for more updates on Blog>

FAQs

How do I download an entire s3 bucket?

You can download entire s3 bucket using the command aws s3 sync s3://full_s3_bucket_name/ your_local_directory

How to download a folder from AWS S3?

You can download a folder from AWS S3 using the command aws s3 sync s3://full_s3_bucket_name/folder_name/ your_local_directory

How do I download multiple files from AWS S3 Bucket?

You can download multiple files from AWS S3 using the command aws s3 cp s3://existing_bucket_name ./destination –recursive

warning: Skipping file E:\\System Volume Information. File/Directory is not readable.

Ensure you are using the local system destination is a folder and not a directory.

Leave a Comment

Share via
Copy link
Powered by Social Snap