S3 Programmatic Access With AWS-CLI

S3 Programmatic Access With AWS-CLI

Day 43 of #90DaysOfDevOps

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

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.


Solution-01

  • Create one EC2 instance with Ubuntu and add user data to update os and install AWS CLI.

  • It will take some time to start the instance. Till then search for S3 in AWS management console and click on it.

  • Click on "Create bucket".

  • Enter bucket name which has to be unique. Then scroll down and click on create.

  • Click on the bucket name and upload a file.

  • Click on upload, then add file and select a file.

  • Now, go back to AWS EC2 dashboard and connect the instance.

  • To verify the installation of AWS CLI, enter aws --version

  • Configure AWS CLI.

  • Fetch S3 bucket objects with the command aws s3 ls s3://bucket-name

  • Now to download the file enter the command aws s3 cp s3://myawsbucket-for-developer001/File-One.txt .

  • Verify by the cat command.


Solution-02

  • Create a snapshot of the existing running instance.

  • Go to snapshot from the left panel of the EC2 dashboard and then create a snapshot.

  • Next, select "instance" and the instance that we just used to connect the S3.

  • Scroll down and click on "create snapshot".

  • Select the snapshot, click on "Actions" and "Create image from snapshot".

  • Then enter image name.

  • Scroll down and click on "create".

  • Now go to AMIs (Amazon Machine Images) from the left panel of the EC2 dashboard and then select image.

  • Then click on "Launch instance".

  • Enter name and launch.

  • Now connect the instance through the SSH. And configure the AWS cli.

  • Then copy the file from the bucket to local storage.

  • Cat the file to verify.


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.

Thank You.