Sign In
Sign In

AWS CLI is a command-line interface for working with Amazon services. It provides a set of commands for managing files in cloud storage.

Installation

To install AWS CLI on Ubuntu, use the command:

sudo apt install awscli -y

After installation, you can check the AWS version with the command:

aws --version

You can find information on installing AWS CLI on other operating systems in the AWS documentation.

Configuration

To configure AWS to work with our S3 object storage, use the command:

aws configure

An interactive prompt will appear, asking for connection credentials. Enter them as follows, using the values from your bucket settings in your control panel:

AWS Access Key ID [None]: <Access Key>
AWS Secret Access Key [None]: <Secret Access Key>
Default region name [None]: us-2
Default output format [None]: json

This will create a hidden .aws directory with config and credentials files, which contain AWS settings and authorization keys, respectively.

Working with Files

Retrieving a List of Files in a Bucket

To retrieve a list of files, use the ls command:

aws s3 ls s3://<bucket-name> --endpoint-url https://s3.hostman.com

Uploading a Local File to the Bucket

To upload files, use the cp command. In the example below, the test.txt file is uploaded:

aws s3 cp test.txt s3://<bucket-name> --endpoint-url https://s3.hostman.com

Deleting a File in the Bucket

To delete a file, use the rm command. In the example below, we delete test.txt:

aws s3 rm s3://<bucket-name>/test.txt --endpoint-url https://s3.hostman.com

Synchronizing Files

To sync files between a local directory (in this example, the current directory .) and the bucket, use the sync command:

aws s3 sync . s3://<bucket-name> --endpoint-url https://s3.hostman.com

You can find detailed information on these and other commands in the AWS documentation.

Multipart Upload

To perform a multipart upload using the AWS CLI, you need to use the aws s3api command. This process involves creating and completing a multipart upload in a few steps.

Preparation

Before performing a multipart upload, the file must be split into parts. You can use the split utility to divide the file:

split -b <part_size> <path_to_file> <part_name_prefix>
  • <part_size>: The size of each part (e.g., for 5 MB, use 5M).

  • <path_to_file>: Path to the source file.

  • <part_name_prefix>: Prefix for part names. For example, if you specify part, the parts will be named partaa, partab, and so on.

Step 1. Initialize the Upload

Start the multipart upload to get an UploadId, which is needed for subsequent steps:

aws s3api create-multipart-upload --bucket <bucket_name> --endpoint-url https://s3.hostman.com --key <file_name_in_bucket>

The command's output will include an UploadId. Save it, as it will be used for uploading parts and completing the process.

Step 2. Upload Parts

Use the upload-part command to upload each file part. You need to specify the UploadId, PartNumber, and the file part path. For example, to upload the first part:

aws s3api upload-part --bucket <bucket_name> --endpoint-url https://s3.hostman.com --key <file_name_in_bucket> --part-number 1 --body <path_to_part> --upload-id <UploadId>

Repeat this command for each part of the file, incrementing the --part-number and updating --body with the appropriate file part.

Step 3. Complete the Upload

After all parts are uploaded, use the complete-multipart-upload command. First, create a JSON file with details about the uploaded parts, including the ETag and PartNumber:

{
  "Parts": [
    {
      "ETag": "<ETag_of_part_1>",
      "PartNumber": 1
    },
    {
      "ETag": "<ETag_of_part_2>",
      "PartNumber": 2
    }
  ]
}

Then, complete the upload:

aws s3api complete-multipart-upload --bucket <bucket_name> --endpoint-url https://s3.hostman.com --key <file_name_in_bucket> --upload-id <UploadId> --multipart-upload file://<path_to_JSON_file>

Abort the Upload

If you want to cancel the upload, use the following command:

aws s3api abort-multipart-upload --bucket <bucket_name> --endpoint-url https://s3.hostman.com --key <file_name_in_bucket> --upload-id <UploadId>

List Multipart Upload Parts

To check the parts uploaded so far, use the list-parts command:

aws s3api list-parts --bucket <bucket_name> --endpoint-url https://s3.hostman.com --key <file_name_in_bucket> --upload-id <UploadId>

Presigned URLs

Presigned URLs allow you to create temporary links for accessing private objects in S3. These links provide access to an object without requiring authentication, and unlike changing an object's ACL, presigned URLs always have an expiration time (maximum 7 days).

Generating a Presigned URL

Use the presign command to generate a temporary link:

aws s3 presign s3://<bucket_name>/<file_path> --endpoint-url https://s3.hostman.com

To set a custom expiration time, use the --expires-in flag, which specifies the time in seconds (default is 3600 seconds):

aws s3 presign s3://<bucket_name>/<file_path> --expires-in <time_in_seconds> --endpoint-url https://s3.hostman.com

How Presigned URLs Work

Presigned URLs use AWS’s standard authorization mechanism. The difference is that the authorization values, typically sent as headers, are appended to the URL as query parameters. This allows external users to access the link via a browser or other client without additional setup. For example, a generated link might look like this:

https://s3.hostman.com/841b4a72-presigned/README.md?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=

 

Was this page helpful?
Updated on 30 October 2024

Do you have questions,
comments, or concerns?

Our professionals are available to assist you at any moment,
whether you need help or are just unsure of where to start.
Email us
Hostman's Support