S3 Programmatic Access with AWS-CLI

S3 Programmatic Access with AWS-CLI

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. You can read more HERE

Task-01

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

Connect to your EC2 Instance using SSH:

ssh -i path/to/your-key-pair.pem ubuntu@[YOUR_EC2_PUBLIC_IP]

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

Create a New S3 Bucket: Click the "Create bucket" button and input a name for your bucket. Choose a region for your bucket. Configure the other options as needed. Then click "Create bucket."

  • Then click "Create bucket."

  • Once your bucket is created, click on its name to open it.

  • Click the "Upload" button.

  • Click "Add files" and select the file you want to upload.

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

Install AWS CL

sudo apt update
sudo apt install awscli

Configure AWS CLI: Run aws configure and enter your AWS Access Key ID, Secret Access Key, region, and output format as prompted.

Use the aws s3 cp command to download the file from your S3 bucket to your EC2 instance.

aws s3 cp s3://BUCKET_NAME/PATH/TO/FILE /PATH/TO/LOCAL/FILE

Task-02

  • 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.

Choose "Snapshots" on the left hand side and click on "Create snapshot"

Select the instance you want to create a snapshot of and click on Create snapshot

Use the newly created Snapshot to launch a new EC2 Instance: Click on "Actions" and select "Create image from snapshot". Follow the prompts to create an Amazon Machine Image (AMI) from your instance.

  • In the EC2 Dashboard, select "AMIs" from the left-hand side, Choose the AMI you created in the previous step.

  • Click "Launch Instance" and follow the instance creation instructions to launch a new instance.

Connect to the EC2 instance using SSH:

Connect as user "ubuntu"

ssh -i "Dev.pem" ubuntu@ec2-54-175-105-14.compute-1.amazonaws.com

  • 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.

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.