Day 30: S3 Programmatic access with AWS CLI

Day 30: S3 Programmatic access with AWS CLI

Hello Everyone!

In the last article, we established a connection between AWS CLI and Windows machine. Let's dive deeper this time and explore the most commonly used service in AWS i.e. S3.

S3

Amazon Simple Storage Service (Amazon S3) is an object storage service that provides a secure and scalable way to store and access data on the cloud. It is designed for storing any kind of data, such as text files, images, videos, backups, and more.

Task-01

  • Launch an EC2 instance using the AWS Management Console and connect to it using Secure Shell (SSH).

  • Create an S3 bucket and upload a file to it using the AWS Management Console.

  • Access the file from the EC2 instance using the AWS Command Line Interface (AWS CLI).

Launch an EC2 instance using the AWS Management Console and connect to it using Secure Shell (SSH).

Create an S3 bucket and upload a file to it using the AWS Management Console.

Let us upload a file to the bucket we created just now.

Click Open the bucket you just created. And in the objects section click on Upload.

You can see that the File upload was successful.

To check the S3 buckets present:

aws s3 ls

Let’s create a file in the instance and upload it to the S3 using CLI.

echo "This is for testing purposes" > sample2.txt
ls
cat sample2.txt
aws s3 cp sample2.txt s3://bucket5476

Verify if the file is uploaded to the AWS console

To list the objects in an S3 bucket:

aws s3 ls s3://bucket5476

Task 2) Create a snapshot of the EC2 instance and use it to launch a new EC2 instance. Download a file from the S3 bucket using the AWS CLI. Verify that the contents of the file are the same on both EC2 instances.

  • Search EC2 in the console > Scroll down to EBS (Elastic Block Store) > Select Snapshots.

  • Click on Create Snapshot > Select Instance > Select the Instance ID > Click on Create Snapshot

Let us use this Snapshot to launch a new EC2 instance.

Select the Snapshot > Click on Actions > Select Create image from the snapshot.

Click on Create Image. In the AMI section, you can observe that the AMI is created

Select the Instance > Actions > Click on Launch Instance from AMI

We can verify that in both instances the files are the same.

aws s3 ls

ec2-instance 1

ec2-instance 2

And that’s how we can access and modify S3 and its contents through CLI.

Here are some commonly used AWS CLI commands for Amazon S3:

aws s3 ls - This command lists all of the S3 buckets in your AWS account.

aws s3 mb s3://bucket-name - This command creates a new S3 bucket with the specified name.

aws s3 rb s3://bucket-name - This command deletes the specified S3 bucket.

aws s3 cp file.txt s3://bucket-name - This command uploads a file to an S3 bucket.

aws s3 cp s3://bucket-name/file.txt . - This command downloads a file from an S3 bucket to your local file system.

aws s3 sync local-folder s3://bucket-name - This command syncs the contents of a local folder with an S3 bucket.

aws s3 ls s3://bucket-name - This command lists the objects in an S3 bucket.

aws s3 rm s3://bucket-name/file.txt - This command deletes an object from an S3 bucket.

aws s3 presign s3://bucket-name/file.txt - This command generates a pre-signed URL for an S3 object, which can be used to grant temporary access to the object.

aws s3api list-buckets - This command retrieves a list of all S3 buckets in your AWS account, using the S3 API.


Thanks for reading ;)