Introduction

AWS CLI provides two different commands to interact with AWS S3. These commands are:

  • s3
  • s3api

The s3 command is easier to use but supports a limited set of functionality. s3api, on the other hand, supports all of the functionality supported by AWS S3. This article covers the differences in more details.

In this tutorial, we will look at multiple use cases where you can use s3api to download a file from S3.

Table of contents

get-object

get-object retrieves objects from Amazon S3. You must have READ access to the object to be able to access it.

The following parameters are most often used with this command:

  • bucket (required): Name of the bucket containing the object
  • key (required): Key of the object to get
  • if-modified-since: Returns an object only if it has been modified since this timestamp
  • range: Download specified range bytes of the object
  • version-id: Download specific version of the object if versioning is enabled

Download an object from S3

We can download an object from S3 using the following command:


aws s3api get-object --bucket YOUR_BUCKET --key path/to/key path/to/local

Output:

{
    "AcceptRanges": "bytes",
    "LastModified": "2022-08-20T22:18:16+00:00",
    "ContentLength": 58,
    "ETag": "\"4409558fcf3eaf77a367cbf231698cbe\"",
    "VersionId": "null",
    "ContentType": "text/csv",
    "Metadata": {}
}

Download a specific version of the object from S3

We can specific the version id of the object that we want to download.


aws s3api get-object --bucket YOUR_BUCKET --key path/to/key --version-id version_id path/to/local

Download a specific range of bytes from S3

We can use the following command to download a fixed range of bytes of the object from S3:

aws s3api get-object --bucket YOUR_BUCKET --key path/to/key --range bytes=10-20 path/to/local

Output:

{
    "AcceptRanges": "bytes",
    "LastModified": "2022-08-20T22:18:16+00:00",
    "ContentLength": 11,
    "ETag": "\"4409558fcf3eaf77a367cbf231698cbe\"",
    "VersionId": "null",
    "ContentRange": "bytes 10-20/58",
    "ContentType": "text/csv",
    "Metadata": {}
}

Download a file that has been modified recently

We can use the following command to download a file only if has been modified since the specified timestamp.

aws s3api get-object --bucket YOUR_BUCKET --key path/to/key --if-modified-since 2022-08-01 path/to/local

Output:


{
    "AcceptRanges": "bytes",
    "LastModified": "2022-08-24T17:48:43+00:00",
    "ContentLength": 26,
    "ETag": "\"a834c5937359d6296139f904294598a3\"",
    "VersionId": "null",
    "ContentType": "text/csv",
    "Metadata": {}
}