Amazon S3 Management Using AWS CLI with LocalStack

Useme Alehosaini
3 min readNov 28, 2024

Using AWS CLI with LocalStack provides a great way to simulate AWS services locally, making it perfect for development and testing without requiring actual access to the AWS cloud. Here’s a guide on how to interact with S3 buckets and objects using the AWS CLI in conjunction with LocalStack.

Prerequisites:

For this guide, we assume that you have LocalStack running on http://localhost:4566 and an AWS CLI profile called localstack.

Command Structure:

All commands will start with:

aws --profile localstack --endpoint-url=http://localhost:4566

This tells AWS CLI to use the localstack profile and connect to LocalStack's endpoint at localhost:4566.

Managing Buckets

Create an S3 Bucket:
To create a new S3 bucket using the AWS CLI with LocalStack:

aws --profile localstack --endpoint-url=http://localhost:4566 s3 mb s3://my-local-bucket

This command creates a bucket named my-local-bucket. The mb stands for "make bucket."

Delete an S3 Bucket:
To delete the bucket, the bucket must be empty. First, ensure there are no objects in the bucket (we will cover that in the next steps). Then run:

aws --profile localstack --endpoint-url=http://localhost:4566 s3 rb s3://my-local-bucket --force

The rb stands for "remove bucket", and the --force flag ensures that the bucket is deleted even if it contains objects.

Managing Objects

Upload an Object to the Bucket:
To upload an object to the S3 bucket, you can use the s3 cp (copy) command:

aws --profile localstack --endpoint-url=http://localhost:4566 s3 cp "C:\repos\Testfiles\my-object.txt" s3://my-local-bucket/

This command uploads a file named my-object.txt from your local machine to the my-local-bucket in LocalStack.

List Objects in the Bucket:
To list all objects in the bucket:

aws --profile localstack --endpoint-url=http://localhost:4566 s3 ls s3://my-local-bucket/

This command will list all files stored in the my-local-bucket.

Update an Object in the Bucket:

aws --profile localstack --endpoint-url=http://localhost:4566 s3 cp "C:\repos\Testfiles\updated-my-object.txt" s3://my-local-bucket/my-object.txt

This will replace my-object.txt with updated-my-object.txt in the S3 bucket.

Download an Object from the Bucket:
To download an object from the bucket to your local machine, use the s3 cp command again, but this time with the source as the S3 bucket and the destination as a local path:

aws --profile localstack --endpoint-url=http://localhost:4566 s3 cp s3://my-local-bucket/my-object.txt ./downloadedfile.txt

This will download my-object.txt from the bucket and save it locally as downloadedfile.txt.

Delete an Object from the Bucket:
To delete an object from the bucket, use the s3 rm command:

aws --profile localstack --endpoint-url=http://localhost:4566 s3 rm s3://my-local-bucket/my-object.txt

This will remove the object my-object.txt from the bucket.

Working with Metadata Objects:

Metadata in S3 objects can be managed through the aws s3api command set. You can add custom metadata when uploading an object or retrieve the metadata of an existing object.

Add Metadata When Uploading an Object:
To upload an object with custom metadata:

aws --profile localstack --endpoint-url=http://localhost:4566 s3api put-object --bucket my-local-bucket --key myfile.txt --body "C:\repos\Testfiles\myfile.txt" --metadata key1=value1,key2=value2

This command uploads myfile.txt to my-local-bucket and assigns custom metadata to it.

Get Object Metadata:
To retrieve the metadata of an existing object, use the head-object command:

aws --profile localstack --endpoint-url=http://localhost:4566 s3api head-object --bucket my-local-bucket --key myfile.txt

This will return metadata details for myfile.txt, including the custom metadata that was set during upload.

Update Object Metadata:
To update metadata on an object, you would need to re-upload the object with the new metadata because S3 does not allow direct modification of metadata without overwriting the object. For example:

aws --profile localstack --endpoint-url=http://localhost:4566 s3api copy-object --copy-source my-local-bucket/myfile.txt --bucket my-local-bucket --key myfile.txt --metadata key1=newvalue --metadata-directive REPLACE

This command copies the object within the same bucket but replaces the metadata with the new value key1=newvalue.

As a summary, with LocalStack and the AWS CLI, you can simulate and test S3 operations locally, including creating and deleting buckets, uploading and downloading objects, and managing metadata. This approach makes it easy to develop and test applications that interact with S3 without needing to connect to the live AWS environment.

Done! 😊
💡 Please let me know your opinion in the comments.

Thank you for your time 😊

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Useme Alehosaini
Useme Alehosaini

Written by Useme Alehosaini

A Lifelong learner, passionate about self-improvement, soft skills, technology and finance. LinkedIn https://www.linkedin.com/in/useme-mba-msc/

No responses yet

Write a response