The AWS Command Line Interface (AWS CLI) is a set of command-line tools provided by Amazon Web Services (AWS) to interact with and manage AWS services and resources directly from your terminal or command prompt. It allows developers, system administrators, and AWS users to perform various tasks such as creating and managing EC2 instances, configuring Amazon S3 buckets, monitoring AWS resources, and more, all from the command line.

You can use the AWS CLI in scripts and automation workflows to perform tasks like creating, managing, and monitoring AWS resources. This is valuable for infrastructure as code (IaC) and DevOps practices.

Protect Your Data with BDRSuite

Cost-Effective Backup Solution for VMs, Servers, Endpoints, Cloud VMs & SaaS applications. Supports On-Premise, Remote, Hybrid and Cloud Backup, including Disaster Recovery, Ransomware Defense & more!

Some of the operations that you can perform with AWS CLI for S3 is given below.

  • Create a S3 bucket
  • List buckets and objects
  • Delete S3 bucket
  • Delete objects
  • Move objects
  • Copy objects
  • Sync objects

Configuring AWS CLI

Configuring the AWS Command Line Interface (AWS CLI) is the first step that allows you to interact with AWS services from your command line or terminal.

You have to create an access key and secret in AWS IAM console for a user that you intend to use for CLI operations.

Download Banner

$ aws configure –profile default
AWS Access Key ID [****************GDS5]: ABCD6729HDSDJFHJSJ7
AWS Secret Access Key [****************QKKK]: kHtYEIKJHAKSHDJKAHSK88+-SJHD
Default region name [us-east-1]:
Default output format [text]:

Top 7 AWS CLI S3 Commands

How to Create a S3 bucket using AWS CLI

1) Create S3 Bucket: AWS CLI command – ‘mb‘ is used to create a bucket.

Syntax: aws s3 mb s3://bucket-name

Example:

$ aws s3 mb s3://demo-ux-s3-01
make_bucket: demo-ux-s3-01

How to List S3 Buckets using AWS CLI

2) List S3 buckets and objects: AWS CLI command – ‘ls‘ is used to list the buckets and objects

Syntax: aws s3 ls [–options]

Example:

  • List the buckets

$ aws s3 ls
2023-10-14 09:43:36 demo-ux-s3-01
$

  • List the objects in a bucket

$ aws s3 ls demo-ux-s3-01
$

Note: There are no objects in the bucket. Hence, there is no output for the above command.

3) Copy objects

AWS CLI command – ‘cp‘ is used to copy objects from one bucket to another or from local machine to S3 bucket.

Syntax: aws s3 cp

Example:

  • Copy the object from local system to S3 bucket

$ aws s3 cp s3-demo-file s3://demo-ux-s3-01
upload: ./s3-demo-file to s3://demo-ux-s3-01/s3-demo-file
$

$ aws s3 ls demo-ux-s3-01
2023-10-15 23:05:04 0 s3-demo-file
$

  • Copy the object from S3 bucket demo-ux-s3-01 to demo-ux-s3-02

$ aws s3 ls demo-ux-s3-02
$

$ aws s3 cp s3://demo-ux-s3-01/s3-demo-file s3://demo-ux-s3-02/s3-demo-file-02
copy: s3://demo-ux-s3-01/s3-demo-file to s3://demo-ux-s3-02/s3-demo-file-02
$

$ aws s3 ls demo-ux-s3-02
2023-10-15 23:08:14 0 s3-demo-file-02
$

4) Move objects

AWS CLI command – ‘mv‘, is used to move the objects from a bucket or local machine to S3 bucket.

While moving the object, the object is deleted from the source bucket and moved to the target bucket.

Syntax: aws s3 mv

Example:

$ aws s3 ls s3://demo-ux-s3-01
2023-10-15 23:05:04 0 s3-demo-file
2023-10-15 23:13:47 0 s3-demo-file-mv-01
$
$ aws s3 mv s3://demo-ux-s3-01/s3-demo-file-mv-01 s3://demo-ux-s3-02/
move: s3://demo-ux-s3-01/s3-demo-file-mv-01 to s3://demo-ux-s3-02/s3-demo-file-mv-01
$
$ aws s3 ls s3://demo-ux-s3-01
2023-10-15 23:05:04 0 s3-demo-file
$
$ aws s3 ls s3://demo-ux-s3-02
2023-10-15 23:08:14 0 s3-demo-file-02
2023-10-15 23:14:44 0 s3-demo-file-mv-01
$

5) Sync Objects

AWS CLI command – ‘sync’, is used to sync the objects from a bucket or local machine to S3 bucket. Any files that are newly created or changed in the source are synced to the destination bucket.

Syntax: aws s3 sync

Example:

In this example, we are syncing the objects from the source S3 bucket demo-ux-s3-01 to bucket demo-ux-s3-02

$ aws s3 ls demo-ux-s3-01
2023-10-15 23:05:04 0 s3-demo-file
2023-10-15 23:20:32 0 s3-demo-file-sync-01
2023-10-15 23:20:41 0 s3-demo-file-sync-02
$
$ aws s3 ls demo-ux-s3-02
2023-10-15 23:08:14 0 s3-demo-file-02
2023-10-15 23:14:44 0 s3-demo-file-mv-01
$
$ aws s3 sync s3://demo-ux-s3-01/ s3://demo-ux-s3-02/
copy: s3://demo-ux-s3-01/s3-demo-file to s3://demo-ux-s3-02/s3-demo-file
copy: s3://demo-ux-s3-01/s3-demo-file-sync-02 to s3://demo-ux-s3-02/s3-demo-file-sync-02
copy: s3://demo-ux-s3-01/s3-demo-file-sync-01 to s3://demo-ux-s3-02/s3-demo-file-sync-01
$
$ aws s3 ls demo-ux-s3-02
2023-10-15 23:23:49 0 s3-demo-file
2023-10-15 23:08:14 0 s3-demo-file-02
2023-10-15 23:14:44 0 s3-demo-file-mv-01
2023-10-15 23:23:50 0 s3-demo-file-sync-01
2023-10-15 23:23:50 0 s3-demo-file-sync-02
$

6) Delete objects

AWS CLI command – ‘rm‘, is used to delete the objects from a bucket

Example:

In this example, we are removing the objects from the S3 bucket.

$ aws s3 ls demo-ux-s3-02
2023-10-15 23:23:49 0 s3-demo-file
2023-10-15 23:08:14 0 s3-demo-file-02
2023-10-15 23:14:44 0 s3-demo-file-mv-01
2023-10-15 23:23:50 0 s3-demo-file-sync-01
2023-10-15 23:23:50 0 s3-demo-file-sync-02
$
$ aws s3 rm s3://demo-ux-s3-02/s3-demo-file
delete: s3://demo-ux-s3-02/s3-demo-file
$
$ aws s3 ls demo-ux-s3-02
2023-10-15 23:08:14 0 s3-demo-file-02
2023-10-15 23:14:44 0 s3-demo-file-mv-01
2023-10-15 23:23:50 0 s3-demo-file-sync-01
2023-10-15 23:23:50 0 s3-demo-file-sync-02
$

How to Delete S3 Bucket from AWS CLI

7) Delete buckets

AWS CLI command – ‘rb’, is used to delete a bucket. Before deleting a bucket, ensure that the files are deleted. Else, the delete operation will fail.

Syntax: aws s3 rb

Example:

In this example, we are deleting a s3 bucket.

$ aws s3 ls
2023-10-14 09:43:36 demo-ux-s3-01
2023-10-15 23:06:32 demo-ux-s3-02
$
$ aws s3 rb s3://demo-ux-s3-02
remove_bucket failed: s3://demo-ux-s3-02 An error occurred (BucketNotEmpty) when calling the DeleteBucket operation: The bucket you tried to delete is not empty
$
$ aws s3 ls s3://demo-ux-s3-02
2023-10-15 23:08:14 0 s3-demo-file-02
2023-10-15 23:14:44 0 s3-demo-file-mv-01
2023-10-15 23:23:50 0 s3-demo-file-sync-01
2023-10-15 23:23:50 0 s3-demo-file-sync-02
$
$ aws s3 rm s3://demo-ux-s3-02/s3-demo-file-02
delete: s3://demo-ux-s3-02/s3-demo-file-02
$ aws s3 rm s3://demo-ux-s3-02/s3-demo-file-mv-01
delete: s3://demo-ux-s3-02/s3-demo-file-mv-01
$ aws s3 rm s3://demo-ux-s3-02/s3-demo-file-sync-01
delete: s3://demo-ux-s3-02/s3-demo-file-sync-01
$ aws s3 rm s3://demo-ux-s3-02/s3-demo-file-sync-02
delete: s3://demo-ux-s3-02/s3-demo-file-sync-02
$
$ aws s3 rb s3://demo-ux-s3-02
remove_bucket: demo-ux-s3-02
$
$ aws s3 ls
2023-10-14 09:43:36 demo-ux-s3-01
$

Conclusion

In summary, the AWS CLI for S3 is an essential tool for anyone working with Amazon S3, whether you’re a developer, system administrator, or data engineer. Its robust features and scripting capabilities empower you to efficiently manage your S3 resources and integrate them into your AWS ecosystem. To make the most of this tool, it’s important to understand the AWS CLI’s commands and options and stay updated with AWS service changes and enhancements.

Follow our Twitter and Facebook feeds for new releases, updates, insightful posts and more.

Rate this post