Creating an S3 Bucket and Uploading Objects Using CLI and API

Madhav Prajapati
4 min readJul 3, 2023

--

madhav Prajapati

Title: Creating an S3 Bucket and Uploading Objects Using CLI and API

Introduction: Amazon Simple Storage Service (S3) is a highly scalable and secure object storage service provided by Amazon Web Services (AWS). In this blog post, we will explore two methods for creating an S3 bucket and uploading objects: using the Command Line Interface (CLI) and the Application Programming Interface (API). Whether you prefer the convenience of a command-line interface or the programmability of an API, both methods offer efficient ways to manage your S3 storage.

Using CLI: The AWS CLI provides a straightforward way to interact with AWS services from the command line. Follow these steps to create an S3 bucket and upload objects using the CLI:

  1. Install and configure the AWS CLI: Download and install the AWS CLI based on the official AWS CLI documentation. Then, configure the CLI with your AWS Access Key ID, Secret Access Key, default region, and output format.
  2. Create an S3 bucket: Open your terminal or command prompt and execute the following command to create an S3 bucket:
aws s3 mb s3://your-bucket-name

Replace “your-bucket-name” with the desired name for your bucket.

  1. Uploading objects to an S3 bucket: Once your bucket is created, you can upload objects to it using the CLI. Make sure you have the object file(s) ready on your local machine and execute the following command:

Uploading Objects to an S3 Bucket using CLI:

Replace “/path/to/local/file” with the local file path of the object you want to upload, and “your-bucket-name” with the name of your S3 bucket.

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

Below is the screenshot of the entire code implemented by me

Using API

AWS provides SDKs that allow programmatic interaction with S3. Here, we’ll focus on using the Boto3 SDK with Python:AWS provides various SDKs that allow you to interact with S3 programmatically. Let’s explore how to create an S3 bucket using the AWS API, using Python, and the Boto3 SDK:

Install the Boto3 library:

  1. Install the Boto3 library: Ensure Python is installed on your system, then install Boto3 by running the following command:
pip install boto3

Create an S3 bucket:

Create an S3 bucket: In your preferred Python editor or IDE, import the Boto3 library and write the code to create an S3 bucket:

import boto3

s3 = boto3.client('s3', region_name='ap-south-1')
s3.create_bucket(Bucket='mybucketbyboto31', CreateBucketConfiguration={'LocationConstraint': 'ap-south-1'})

Replace “your-bucket-name” with your desired bucket name.

Run the code:
1. Save the Python script with a meaningful name.
2. Execute the script, and the S3 bucket creation process will be initiated using the specified AWS API.

Madhav Prajapati

Uploading Objects to an S3 Bucket using Boto3:

Uploading objects to an S3 bucket: After creating the bucket, you can upload objects programmatically. Modify the Python script created earlier or create a new one with the following code:

import boto3
s3 = boto3.client('s3')
s3.upload_file('/path/to/local/file', 'your-bucket-name', 'object-key')

Replace “/path/to/local/file” with the local file path of the object, “your-bucket-name” with your bucket’s name, and “object-key” with the desired key or filename for the object.

  • Save the Python script and run it.
  • The specified object will be uploaded to the S3 bucket using the AWS API.
Madhav Prajapati

So We created the bucket by AWS CLI and the boto3 library, you can see the output below.

Conclusion: In this blog post, we explored two methods for creating an S3 bucket and uploading objects: using the AWS CLI and the Boto3 API with Python. Both approaches offer efficient ways to manage your S3 storage, allowing you to choose the method that best fits your workflow and requirements. Whether you prefer the simplicity of CLI commands or the programmability of API calls, AWS provides versatile options for managing your S3 resources effectively.

Connect with me on LinkedIn if you have any doubts:-
https://www.linkedin.com/in/madhavprajapati/

I hope this explanation helps clarify the steps involved

I hope this article will be useful for you and you learned something new.

thank you : )

#vimal daga #righteducation #keeplearning & #keepsharing

keep learning keep sharing (❁´◡`❁)

--

--