Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DevOps Lifecycle
  • DevOps Roadmap
  • Docker Tutorial
  • Kubernetes Tutorials
  • Amazon Web Services [AWS] Tutorial
  • AZURE Tutorials
  • GCP Tutorials
  • Docker Cheat sheet
  • Kubernetes cheat sheet
  • AWS interview questions
  • Docker Interview Questions
  • Ansible Interview Questions
  • Jenkins Interview Questions
Open In App
Next Article:
How to Create Subfolders in S3 Bucket using AWS CLI
Next article icon

Create AWS S3 Bucket using CLI

Last Updated : 27 Nov, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Amazon S3 is a Simple Storage Service in AWS that stores files of different types like Photos, Audio, and videos as objects providing more scalability and security. It allows users to store and retrieve any amount of data at any point in time from anywhere on the web. It facilitates features such as extremely high availability, security, and simple connections to other AWS Services. In this article, we will see how to create s3 buckets using AWS CLI.

Prerequisites:

  • Set up an Amazon Web Console. Refer to the article - AWS Console
  • IAM user with appropriate access. Refer to the article - IAM User
  • Configure a CLI profile using your access key, secret key, default region, and output format. Refer to the article - AWS CLI

Creating S3 Bucket Using CLI

The AWS S3 command is part of the AWS Command Line Interface (CLI) and serves as a tool for interacting with Amazon Simple Storage Service (S3). The AWS S3 make bucket command creates a new bucket in Amazon S3. It's a straightforward way to initiate the creation of a storage container within AWS. mb stands for make bucket The bucket name should be globally unique

aws s3 mb s3://<bucket-name> [--options]

Step 1: We will create a S3 bucket named gfg-example with a default region. Before command execution

s3 buckets before command execution
s3 buckets before command execution

aws s3 mb s3://gfg-example

Step 2: The above command will create a s3 bucket with default region

S3 bucket creation
S3 bucket creation
s3 buckets after command execution
s3 buckets after command execution

Step 3: Here, we are creating a s3 bucket with specific region using AWS CLI.

The --region flag in the aws s3 mb command allows us to specify the region of the S3 bucket. By using this flag we can ensure that the s3 bucket is belongs to specific AWS region. Here I am creating a bucket in the Mumbai (ap-south-1) region.

aws s3 s3://gfg-example2 --region ap-south-1

S3 bucket with specific region
s3 bucket with specific region
s3 bucket
s3 bucket

How to List All AWS S3 Buckets?

Use the following command to list all AWS S3 Buckets:

aws s3 ls

Use the following command to list all the files within a bucket:

aws s3 ls s3://my-bucket-name

Uploading Or Downloading Objects From S3 Bucket using AWS CLI Commands

The aws s3 cp command can be used for both uploading files to S3 bucket and downloading files from S3 bucket.

aws s3 cp <source> <target> [options]

  • source:
    • For uploading files to s3 bucket this option set to local path that you want to upload to s3.
    • For downloading files from s3 bucket this set to S3 URN.
  • target:
    • For uploading files to s3 bucket this set to URN of S3 bucket.
    • For downloading the files from s3 bucket this is set to local path where you want to save the downloaded file.
  • options: These are optional parameters to modify the original behavior of the command. Here are some commonly used optionsal parameters
    • --recursive: If <source> or <target> is a directory then this parameter will recursively upload/downloads the files.
    • --acl: This parameter specifies the access control list for object
    • --sse: Specifies the server-side encryption method for the uploaded object.

Uploading Objects in S3

Step 1: Here, we are uploading the all the files present in directory to s3 bucket(gfg-example)

Objects in s3 before uploading
Objects in s3 before uploading
aws s3 cp <directory-name> s3://gfg-example --recursive
Command execution
Command execution
Objects in s3 after uploading
Objects in s3 after uploading

Downloading Objects From S3

Step 1: In this example we just download the one file from above s3 bucket.

aws s3 cp s3://gfg-example/1.here-there-and-everywhere.pdf C:\Users\ravis\Desktop

Step 2: The above command downloads the 1.here-there-and-everywhere.pdf from gfg-example bucket to local Desktop.

Downloading file
Downloading file

The aws s3 rb command is used to delete s3 bucket and aws s3 rm is used to delete the objects from s3 bucket.

aws s3 rb <S3 URN>

The aws s3 rb deletes the s3 bucket if and only if bucket is empty

aws s3 em <S3 URN>

We can delete all the objects from s3 using --recursive option parameter.

Deleting S3 Bucket Using S3 Command

Step 1: Deleting the one object from s3 bucket(gfg-example)

aws s3 rm s3://gfg-example/1.here-there-and-everywhere.pdf

Step 2: The above command will delete object named with 1.here-there-and-everywhere.pdf

Deleting
Deleting

Step 3: First we try to delete the s3 bucket which is empty(gfg-example) then we will empty the bucket and then delete the s3 bucket(gfg-example).

aws s3 rb s3://gfg-example

Step 4: If the bucket is empty then above command directly delete bucket without any error else it will give BucketNotEmptyError.

aws s3 rm s3://gfg-example --recursive  #first delete all the objects present in given bucket
aws s3 rb s3://gfg-example #delete the bucket
Delete Example
Delete Example

Creating S3 Bucket with Low Level S3 API Command

The s3api is a command that allows users to interact with Amazon Simple Storage Service (S3) through the AWS CLI. With s3api, users can perform advanced operations on S3 buckets and objects such are configure bucket policies, set access control lists (ACLs), manage versioning, configure lifecycle policies, and more.

Creating Bucket

aws s3api create-bucket --bucket <bucket-name> --region <region-name>

  • create-bucket: This subcommand initiates the process of creating a new S3 bucket.
  • --bucket: This option is used to specify the name of the bucket you want to create.
  • --region: This option allows you to specify the AWS region where you want to create the bucket

Step 1: The command will create a s3 bucket with name gfg-eample3 in Europe (Ireland) region

aws s3api create-bucket --bucket gfg-example3 --region eu-west-1

Step 2: Attaching life cycle policy to bucket

aws s3api put-bucket-lifecycle-configuration --bucket <bucket-name> --lifecycle-configuration file://<lifecycle-config-file.json>

  • put-bucket-lifecycle-configuration: This is the subcommand used to set the lifecycle configuration for the specified bucket.
  • --bucket: This option specifies the name of the S3 bucket. s3 bucket should be present in s3.
  • --lifecycle-configuration: This option specifies the lifecycle configuration for the bucket.
  • First we will create json file which contains the life cycle rules for s3 bucket. Refer the document to know how to write life cycle policy for s3 bucket.
  • Then we will attach the Life cycle poliy to gfg-example3 bucket

aws s3api put-bucket-lifecycle-configuration --bucket gfg-example3 --lifecycle-configuration file://C:/Users/ravis/Desktop/life-cycle-policy.json

Life cycle policy
Life cycle policy
Click On Management
Life cycle attachment
Life cycle attachment

Applying ACL Property of S3

Step 1: We can use the put-bucket-acl of s3api for specifying the ACL property of s3 bucket.

aws s3api put-bucket-acl --bucket <bucket-name> --acl <acl>

  • put-bucket-acl: This is the command used to set the access control list for the specified bucket.
  • --bucket: This option specifies the name of the S3 bucket
  • --acl: This option specifies the desired access control list for the bucket. The <acl> parameter can be set to one of the following values:
    • private: Bucket and objects are accessible only to the bucket owner.
    • public-read: Bucket is accessible to anyone, but object access is still controlled by individual object ACLs.
    • public-read-write: Bucket and objects are accessible to anyone. Use with caution.
aws s3api put-bucket-acl --bucket gfg-example3 --acl private
ACL policy
ACL policy

Step 2: Carefully review the bucket policies and IAM Policies in order to execute the commands properly.

Conclusion

Creating an AWS S3 bucket using the CLI is a straightforward process that involves configuring the AWS CLI with your credentials and executing the aws s3api create-bucket command with the necessary parameters, such as the bucket name and region. This method is efficient for automating tasks and managing resources directly from the command line, especially in large-scale environments or automated workflows. By using the CLI, you gain flexibility and control over your AWS resources while ensuring consistent and repeatable operations across your infrastructure.


Next Article
How to Create Subfolders in S3 Bucket using AWS CLI
author
ravi_sadam
Improve
Article Tags :
  • Amazon Web Services
  • DevOps
  • AWS

Similar Reads

  • How To Create GCP Bucket Using CLI ?
    Users can store and manage their data in the cloud with Google Cloud Storage (GCS), the service is provided by the Google Cloud Platform (GCP). Having the big virtual hard drive to store files, documents, photos, movies, and other media is similar to that. Within Google Cloud Storage, a bucket refer
    4 min read
  • How to Create Subfolders in S3 Bucket using AWS CLI
    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
    7 min read
  • How to Create AWS S3 Bucket Using Terraform?
    S3 stands for Simple Storage Service. S3 buckets are cloud storage services by Amazon Web Service. It is used to store objects, It consists of data in any format like documents, images, videos, and application code. These are highly scalable. Prerequisite: AWS AccountTerraform InstallationAWS CLISte
    3 min read
  • How To Aceses AWS S3 Bucket Using AWS CLI ?
    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
    5 min read
  • How to Delete S3 Bucket from AWS CLI ?
    Amazon Simple Storage Service (S3) is a versatile, scalable object storage service given by Amazon Web Services (AWS). It is intended to store and recover any amount of data from any place on the web, offering developers and IT teams a durable, dependable solution for their storage needs. S3 is gene
    6 min read
  • Amazon S3 - Creating a S3 Bucket
    Amazon Simple Storage Service (Amazon S3) or Amazon S3 is an object type, high-speed or with minimal latency, low-cost and scalable storage service provided by AWS. S3 also allows you to store as many objects as you'd like with an individual object size limit of five terabytes. It provides 99.999999
    3 min read
  • Create a Database Using AWS rds
    Make a database on cloud platform using AWS (Amazon Web Services). Introduction to Cloud Cloud computing is an information technology paradigm that enables ubiquitous access to shared pools of configurable system resources and higher-level services that can be rapidly provisioned with minimal manage
    5 min read
  • Create Bucket Policy in AWS S3 Bucket with Python
    Bucket policy of s3 bucket means permission and action which can be applied on the particular bucket. AWS S3 has an optional policy that can be used to restrict or grant access to an S3 bucket resource. It is important to note that bucket policies are defined in JSON format. For creating a bucket po
    2 min read
  • How to Get Object in AWS S3 Using UI & CLI ?
    The Amazon Web Services (AWS) Simple Storage Service (S3) is a scalable object storage service that allows you to store and retrieve any amount of data at any time. The AWS SDK for JavaScript in Node.js provides the getObject method, which can be used to download objects from S3. This article will s
    5 min read
  • How To List All AWS S3 Objects In A Bucket Using Java
    Amazon Web Services provides its Simple Storage service for uploading data to the cloud. Data in S3 is stored as objects inside buckets. This data can then be used as per the bucket policy. Accessing data through code is one means of processing this stored data in the cloud. In the following section
    6 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences