. Advertisement .
..3..
. Advertisement .
..4..
Is there any straightforward method to list S3 buckets in AWS (Amazon Web Services)? Of course, ITtutoria can help you with this! Scroll down to read more instructions on this matter.
How to List S3 Buckets in AWS CLI?
How to list your objects, folders, or buckets? We suggest you opt for the “S3 LS” command. When you use this command without options or targets, it will list out all buckets for you. Here is the basic syntax:
Syntax: $ aws s3 ls <target> [--options]
Let’s look at a more detailed example to help you grasp this concept. In this example, the system has listed all your S3 buckets.
Example 1:
$ aws s3 ls
2018-12-11 17:08:50 my-bucket
2018-12-14 14:55:44 my-bucket2
Meanwhile, this command lists out all prefixes and objects in one bucket. In the example output, this prefix contains one file called “MyFile1.txt”.
Output 1:
$ aws s3 ls s3://bucket-name
PRE example/
2018-12-04 19:05:48 3 MyFile1.txt
It is possible to filter this output to more specific prefixes. How so? You may include them in your commands.
In the next command, the system lists out all objects in bucket-names/examples (what does that mean? It means the bucket-name objects get filtered by a prefix example).
Example 2:
$ aws s3 ls s3://bucket-name/example/
2018-12-06 18:59:32 3 MyFile1.txt
And how about bucket deleting?
For S3 bucket deletion in AWS CLI, you may use “aws s3 rb” or “aws s3api delete-bucket” commands. Still, remember that it is only feasible to delete empty S3 buckets.
Example 3:
aws s3 rb s3://hands-on-cloud-example-1
But what if this S3 bucket has objects? Don’t worry; use the “–force” arguments to extract that bucket before deleting it!
Remember that this “–force argument” is not to delete versioned objects. Otherwise, such operations will render the deletion a failure.
Example 4:
aws s3 rb s3://hands-on-cloud-example-2 --force
So how to delete the bucket with activated objects versioning? Clean it up before following the steps we already mentioned.
Example 5:
export bucket_name="hands-on-cloud-versioning-enabled"
# Deleting objects versions
aws s3api delete-objects \
--bucket $bucket_name \
--delete "$(aws s3api list-object-versions \
--bucket $bucket_name \
--output=json \
--query='{Objects: Versions[].{Key:Key,VersionId:VersionId}}')"
# Deleting delete markers
aws s3api delete-objects \
--bucket $bucket_name \
--delete "$(aws s3api list-object-versions \
--bucket $bucket_name \
--output=json \
--query='{Objects: Contents[].{Key:Key,VersionId:VersionId}}')"
# Deleting S3 bucket
aws s3 rb s3://$bucket_name
Another thing to remember is that result filters are possible with s3 rm, s3 sync, s3 mv, or s3 cp commands. All you need is to turn to the "--include" or "--exclude" options!
"--exclude" only excludes command objects applied in specified orders.
Example 6:
Local directory contains 3 files:
MyFile1.txt
MyFile2.rtf
MyFile88.txt
// Exclude all .txt files, resulting in only MyFile2.rtf being copied
$ aws s3 cp . s3://my-bucket/path --exclude "*.txt"
// Exclude all .txt files but include all files with the "MyFile*.txt" format, resulting in, MyFile1.txt, MyFile2.rtf, MyFile88.txt being copied
$ aws s3 cp . s3://my-bucket/path --exclude "*.txt" --include "MyFile*.txt"
// Exclude all .txt files, but include all files with the "MyFile*.txt" format, but exclude all files with the "MyFile?.txt" format resulting in, MyFile2.rtf and MyFile88.txt being copied
Conclusion
This guideline has instructed you to list S3 buckets in AWS CLI, delete buckets, and filter results. Feel free to ask us for other tips if needed! And if you encounter other errors while using AWS, check out these QA sections for solutions.
Leave a comment