AWS S3 CLI Basic CRUD Operations
This guide will walk you through the basic object lifecycle - Create, Read, Update, and Delete (CRUD) - on an object using the AWS S3 CLI.
In the credentials part of this tutorial, we have created a few buckets tutorial
tutorial2
and tutorial3
. We are going to use them in that tutorial.
AWS CLI manual
The CLI comes with an integrated manual that you can check to see how a particular command works. You can invoke it by adding help
at the end of the command.
For instance let's say we would like to know more about the aws s3api put-object
command, we can use aws s3api put-object help
to invoke the manual.
Lexical
Bucket
: A container for objects. Every object is contained in a bucket.Object
: A piece of data (aka. a blob) and its metadata stored in a bucket.Key
: The name you gave to the object. A key is the unique identifier for an object within a bucket.
Uploading an object
Use the aws s3 cp
command to upload an object in your bucket.
This command stores the string Hello World!
as an object
with the key hello
in the bucket tutorial
.
echo 'Hello World!' > tempFile.txt
aws s3 --profile astran cp tempFile.txt s3://tutorial/hello
You can now delete the temporary file:
rm tempFile.txt
Downloading an object
Use the aws s3 cp
command to download an object from your bucket.
This command retrieves the string we just stored as object hello
in our bucket tutorial
.
Then we print the retrieved content: it should say "Hello World!".
aws s3 --profile astran cp s3://tutorial/hello test.txt
cat test.txt
Overwriting an object
Use the aws s3 cp
command again to overwrite an object in your bucket.
This command overwrites the object test.txt
.
Its new content is "Hello Again, World!".
echo 'Hello Again, World!' > test.txt
aws s3 --profile astran cp test.txt s3://tutorial/hello
rm test.txt
# Download the object again to make sure it was overwritten
aws s3 --profile astran cp s3://tutorial/hello test.txt
cat test.txt
# Should print Hello Again, World!
List all objects in a bucket
Use the aws s3 ls
command to list all the objects in a bucket.
aws --profile astran s3 ls s3://tutorial
Deleting an object
Use the aws s3 rm
command to delete an object from your bucket.
This command deletes the object with the key hello
from the bucket hello
.
aws --profile astran s3 rm s3://tutorial/hello
Deleting a bucket
You've learned how to create a bucket with the aws s3 mb
command in the credentials part of this tutorial. We're now going to show you how to delete a bucket.
Use the aws s3 rb
command to delete a bucket.
aws --profile astran s3 rb s3://tutorial3
You won't get any output but you can list all buckets to check if it worked.
You can only delete an empty bucket. If it contains any object, the delete will fail.
For example if you try the following:
echo 'Hello' > test.txt
aws --profile astran s3 cp test.txt s3://tutorial/hello
aws --profile astran s3api delete-bucket --bucket tutorial
You will get the following error:
remove_bucket failed: s3://tutorial An error occurred (BucketNotEmpty) when calling the DeleteBucket operation (reached max retries: 0): cannot delete the bucket name tutorial: 1 object found
List all buckets
Use the aws s3 ls
command to list all buckets.
aws --profile astran s3 ls
You should only see tutorial
and tutorial2
in the output.
Going Further
To learn more about AWS S3 CLI and its capabilities, you can refer to these resources:
- The AWS Command Line Interface User Guide provides information on how to install, update, and configure AWS CLI.
- The AWS CLI Command Reference is a comprehensive reference guide that describes all available commands for AWS CLI.
These resources should provide you with a comprehensive understanding of how to use AWS S3 CLI effectively.