Now, you will see how to create a bucket, upload an object, and read the object using the AWS CLI:
$ pwd
/Users/baba/AWS-Certified-Machine-Learning-Specialty-
2020-Certification-Guide/Chapter-5/s3demo/demo-files
$ aws s3 ls
$ aws s3 mb s3://demo-bucket-baba –region us-east-1
make_bucket: demo-bucket-baba
$ aws s3 ls
2020-11-04 14:39:50 demo-bucket-baba
$ aws s3 cp sample-file.txt s3://demo-bucket-baba/
upload: ./sample-file.txt to s3://demo-bucket-
baba/sample-file.txt
Figure 2.2 – AWS S3 listing your files
You can also list the files in your S3 bucket from the command line, as shown here:
$ aws s3 ls s3://demo-bucket-baba/
2020-11-04 14:50:02 99 sample-file.txt
$ aws s3 cp .
s3://demo-bucket-baba/ –recursive
upload: folder-1/a.txt to s3://demo-bucket-baba/folder-1/a.txt
upload: folder-2/sample-image.jpg to s3://demo-bucket-baba/folder-2/sample-image.jpg
upload: ./sample-file.txt to s3://demo-bucket-baba/sample-file.txt
$ aws s3 ls s3://demo-bucket-baba/
$ aws s3 mb s3://demo-bucket-baba-copied –region us-east-2
$ aws s3 mb s3://demo-bucket-baba-moved –region us-east-2
$ aws s3 cp s3://demo-bucket-baba s3://demo-bucket-baba-copied/ –recursive
$ aws s3 mv s3://demo-bucket-baba s3://demo-bucket-baba-moved/ –recursive
$ aws s3 ls
2020-11-04 14:39:50 demo-bucket-baba
2020-11-04 15:44:28 demo-bucket-baba-copied
2020-11-04 15:44:37 demo-bucket-baba-moved
$ aws s3 ls s3://demo-bucket-baba/
If all the commands are run successfully, then the original bucket should be empty at the end (as all the files have now been moved).
Note
In the certification exam, you will not find many questions on bucket- and object-level operations. However, it is always better to know the basic operations and the required steps.
$ aws s3 rb s3://demo-bucket-baba
$ aws s3 rb s3://demo-bucket-baba-moved
remove_bucket failed: s3://demo-bucket-baba-moved An error occurred (BucketNotEmpty) when calling the DeleteBucket operation: The bucket you tried to delete is not empty
$ aws s3 rb s3://demo-bucket-baba-moved –force
$ aws s3 rb s3://demo-bucket-baba-copied–force
If you want to delete all the content from a specific prefix inside a bucket using the CLI, then it is easy to use the rm command with the –recursive parameter.
In the next section, you are going to learn about object tags and object metadata.