Open In App

How To Create Subfolders In S3 Bucket Using AWS CLI

Last Updated : 08 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

The AWS Simple Storage Service (S3) is a cloud service provided by Amazon Web Services (AWS) to store your data securely. There are different approaches to storing and retrieving data from AWS S3; one of them is using the AWS CLI provided by Amazon Web Services. In this article, we will provide you with step-by-step instructions on how to use AWS CLI to create subfolders in an AWS S3 bucket.

What Is Amazon S3?

Amazon S3 is a simple storage service provided by Amazon Web Services. It is highly durable and provides security and scalability for any kind of data. This data is stored as objects within resources called buckets.

Creating Subfolders In Amazon S3

Create an AWS account and an IAM role. Identity and Access Management (IAM) helps you secure the services you want to use by providing authentication and authorization as per your team’s requirements. Use the IAM role for accessing the S3 bucket data instead of directly using your main or root account.

Step 1: Create An Amazon S3 Bucket

  • Sign in to your Amazon console and navigate to Amazon S3. Click on Create bucket to proceed.

Amazon S3 bucket console

Step 2: Configure The Amazon S3 Bucket

  • Select an AWS Region and enter a unique bucket name to create a new bucket.

Create AWS S3 Bucket

AWS S3 bucket created

Step 3: Create Folder On Your S3 Bucket

  • Click on upload and create a new folder on the S3 bucket.
  • On successful creation, you’ll see the folder in your bucket’s object list.

AWS S3 folder created

Step 4: Add Files To Your Folder

  • Add few files to your folder by clicking on the upload button.
  • You can see 3 files are added below.

files added to S3 bucket

Step 5: Configure AWS CLI

  • Install AWS CLI on your command line using the below command.
curl "https://awscli.amazonaws.com/AWSCLIV2.pkg" -o "AWSCLIV2.pkg"
sudo installer -pkg AWSCLIV2.pkg -target /

Install AWS CLI on terminal

  • Verify successful installation by checking the version of aws using below command.
aws --version

AWS CLI command for checking aws version

  • Configure your IAM user on the AWS CLI using below command
aws configure

AWS CLI command for configuration

  • Execute below command to list the buckets.
aws s3 ls

AWS CLI command to list buckets

Step 6: Create Sub Folder in AWS S3

  • 6.1: Create a folder on AWS S3
    • Execute the below command to create a folder on the AWS S3 bucket.
    • Provide bucket name and the new folder’s name you want to create on the S3.
aws s3api put-object --bucket <bucket_name> --key <new_folder_name>/ --content-length 0

AWS CLI command to create folder on AWS S3 bucket

  • You’ll get the below output on executing above command.

AWS CLI folder generated response

  • Check if folder is created on AWS S3 console.

Folder created by AWS CLI visible on AWS S3 console

  • 6.2: Create a subfolder on AWS S3
    • Execute the below command to create a sub folder on the AWS S3 bucket.
    • Provide bucket name and the new sub folder’s name you want to create on the S3 along with the path.
aws s3api put-object --bucket <bucket_name> --key <path_to_sub_folder>

AWS CLI command to create sub folder in AWS S3

  • You’ll get the below output on executing above command.

AWS CLI sub folder generated response

  • Verify if sub folder is generated on the existing folder on AWS S3 console.

Sub folder visible on AWS S3 console

  • You can also verify this using AWS CLI by executing below command.
aws s3 ls s3://<bucket_name> --recursive

AWS CLI command to list content of S3 bucket

In this way, you can create sub folder on AWS S3 using AWS CLI.

Create subfolders in S3 bucket using AWS CLI – FAQ’s

Can we create multiple profiles on AWS CLI?

Yes you can create multiple profiles using command

aws configure –profile <profile_name>

How can we make AWS CLI more secure?

You can apply IAM restrictions as well as assign MFA for AWS CLI operations

What is the purpose of AWS CLI?

AWS CLI provides you with a text based interface to perform operations on your AWS account without having to use the AWS console.

Which terminals support AWS CLI?

  • Linux shells program on linux and macOs
  • Windows command prompt or powershell
  • Amazon Elastic Compute Cloud (Amazon EC2) instances through a remote terminal program such as PuTTY or SSH, or with AWS Systems Manager.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads