How to download an object from Amazon S3 using AWS CLI

This post explains how to download objects from AWS S3 bucket using the AWS CLI. This post assumes you have AWS CLI installed and configured, if AWS CLI is not installed or not configured follow below posts to complete the setup.

[On Windows]
1. How to install AWS CLI on windows
2. How to configure AWS CLI

[On Ubuntu]
1. How to install AWS CLI on Ubuntu
2. How to configure AWS CLI

Command to download an object from S3 using AWS CLI.
        

aws s3api get-object --bucket "BUCKET_NAME" --key "OBJECT_KEY" "NAME_OF_OUTPUT_FILE"

    
    
Example to download complete object from S3.
        

aws s3api get-object --bucket test-bucket-001 --key dir/sample_object1.txt sample_object1.txt
    

    

Above command downloads an object with name sample_object1.txt from folder dir in S3 bucket test-bucket-001 and saves the output to the local file sample_object1.txt.

Example command to download first 500 bytes of an object.
        

aws s3api get-object --bucket test-bucket-001 --key dir/sample_object1.txt --range bytes=1-500 sample_bytes.txt
            

            
Above command downloads first 500 bytes of an object with name sample_object1.txt from folder dir in S3 bucket test-bucket-001 and saves the output to the local file sample_bytes.txt.

Categories: AWS