AWS CLI with Amazon S3

Introduction

In this guide, we will walk through how to use the AWS CLI to perform basic operations with Amazon S3, such as creating a bucket, listing objects, and deleting both objects and the bucket.

Step 1: Create an S3 Bucket

To create an S3 bucket using the AWS CLI, execute the following command. Make sure to replace aws-cli-2000 with your desired bucket name.

aws s3 mb s3://aws-cli-2000

AWS CLI

Make sure your bucket name is unique and complies with AWS naming conventions.

Step 2: List All S3 Buckets

After creating the bucket, use the following command to list all the available S3 buckets:

aws s3 ls

AWS CLI

Step 3: List Objects in a Specific Bucket

To view the contents of a specific S3 bucket, use the following command:

aws s3 ls s3://bucket-name

Replace bucket-name with the name of the bucket you want to inspect.

AWS CLI

Step 4: Delete an Object from the Bucket

To remove an object from the S3 bucket, run the following command:

aws s3 rm s3://bucket-name/object

Replace bucket-name with your bucket name and object with the specific object you want to delete.

AWS CLI

Deleting an object is permanent and cannot be undone.

Step 5: Delete the S3 Bucket

Finally, once all objects are removed, you can delete the bucket itself with the command:

aws s3 rb s3://bucket-name

AWS CLI

Ensure the bucket is empty before attempting to delete it. Use --force to delete non-empty buckets, but exercise caution.

Conclusion

By following these steps, you’ve learned how to create, list, and delete S3 buckets and objects using the AWS CLI. These fundamental commands will help you manage your S3 resources efficiently.