Open In App

How To Aceses AWS S3 Bucket Using AWS CLI ?

Last Updated : 05 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

The AWS Command Line Interface (AWS CLI) is an open-source tool that allows you to interact with AWS services using syntax in your shell. It provides you with the ability to quickly and easily access the same functionality as the browser-based AWS Management Console from the comfort of your terminal program.

S3 (Simple Storage Service) is a storage service provided by AWS to store any kind of data in File/Folder format. In this article, we will see how easily we can access S3 to store our data in buckets which are containers to store your data.

Prerequisites

  • AWS Account: Before starting this tutorial, you must have an AWS account. Read this article in case you don’t have an AWS account.
  • AWS CLI: You should have AWS CLI set in your local machine with access. Refer to this article if you want to know how to set up AWS CLI.

Step-By-Step Guide To TO Aceses S3 Bucket Using AWS CLI

Step 1: Login into AWS -> https://aws.amazon.com/ Management

Step 2: After signing in you will land on the AWS Management Console page and search for S3 as shown below.

AWS Management Console

Step 3: From the sidebar go to buckets. A bucket is a container to store the objects (Files and Folders).

S3

Step 4: On the buckets page all of your buckets will be listed.

Buckets

To Create A New S3 Bucket

Step 5: Open Command Prompt/Powershell/Terminal on your system. Type the following command to make an S3 bucket.

Syntax :

aws s3 mb s3://bucket-name 

Make sure the bucket name is between 3 and 63 characters long and must contain only lower-case characters, numbers, periods, and dashes.Also, your bucket name should be unique as they are global in AWS.‘mb’ stands for make bucket in syntax.

Make bucket

Step 6: Goto AWS Management Console and refresh the buckets page, after refreshing you will be able to see your bucket name.

Bucket

To Copy A Single File To An S3 Bucket

Step 7: Let’s add some files in your bucket.

Syntax :

aws s3 cp 'your local file path' s3://bucket-name

‘cp’ stands for copy in syntax.‘bucket-name’ can also be your path, adding ‘/’ in front of the name will go to subfolders in the bucket. The above syntax will allow you to add a single file (not a folder) from the local computer to the S3 bucket.

Copy

To List all S3 buckets and all files present in the S3 bucket

Step 8: To check how many buckets are present in S3 use the following syntax

Syntax :

aws s3 ls

List bucket

Step 9: To check how many files are present in the S3 bucket use the following syntax

Syntax :

aws s3 ls bucket-name

List files

Step 10: Again, go to the AWS Managment Console refresh the buckets page and click on your bucket name to open the bucket. You will be able to see all the files present in your bucket which you copied from local machine.

File in bucket

To Copy Multiple Files/Folders To A S3 Bucket

Step 11: To add multiple files/folders in your bucket just add ‘–recursive‘ at the end of the single copy syntax. This will upload all the files from the given path.

Syntax :

aws s3 cp 'your local file path' s3://bucket-name --recursive

Multiple Files upload

Step 12: Again, go to AWS Managment Console and open your bucket. You will be able to see all the files present in your bucket that you copied from a local machine.You can also check files by listing them in terminal.

Upload files

Delete All Files/Folders From Bucket

Step 13: To delete objects from bucket just add path to bucket to delete everything inside bucket or add specific file/folder name to delete certain object.

Syntax:

aws s3 rm s3://path-to-file-folder

Example: aws s3 rm s3://gfg-s3-test-bucket -–recursive

To delete single file don’t add the ‘–recursive’ command.

Delete

Step 14: Open your bucket in the AWS Management Console, and check if file that you deleted is present or not. You can also use ‘list’ command to check it.

Empty bucket

Delete Bucket

Step 15: To delete the bucket just just add bucket name in front of the ‘rb’ command.

Syntax :

aws s3 rb s3://bucket-name

‘rb’ stands for remove bucket in the syntax.

Remove bucket

Step 16: Check on AWS Management Console. It will bucket ‘bucket-name’ was not found. You can also use ‘list’ command to check it.

Delete massage

Conclusion

In this article, we explored AWS CLI commands to perform various operations such as create, copy delete and list in the AWS S3 bucket. CLI makes it easy to perform tasks using simple commands and arguments.

S3 Bucket -FAQs

What credentials do I need to access an S3 bucket with AWS CLI?

It requires Access Key ID and Secret Access Key, which you can obtain from the AWS Management Console.

Can I access a specific S3 bucket using AWS CLI without configuring it globally?

Yes, you can use the –profile option while creating S3 bucket.

What is the syntax for downloading files from an S3 bucket using AWS CLI?

‘aws s3 cp object-url’ command can be also use to dowload the S3 object by just adding URL of the object.

Is it possible to sync local and S3 directories using AWS CLI?

‘aws s3 sync ./local-path/ s3://bucket-name/’ can be used to sync four bucket in case of any modifications.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads