Creating an S3 bucket and upload objects using both the Command Line Interface (CLI) and Application Programming Interface (API) methods.

Creating an S3 bucket and upload objects using both the Command Line Interface (CLI) and Application Programming Interface (API) methods.

Introduction:

Amazon Simple Storage Service (S3) is a highly scalable and reliable object storage service offered by Amazon Web Services (AWS). It provides developers with an easy-to-use interface to store and retrieve any amount of data from anywhere on the web. In this blog post, we will explore two methods of creating an S3 bucket and uploading objects: using the Command Line Interface (CLI) and the Application Programming Interface (API).


1) Using AWS cli

  • Setting up AWS CLI:

Before we dive into creating an S3 bucket and uploading objects using the CLI, we need to set up the AWS CLI on our local machine. This involves installing the CLI and configuring it with your AWS access key and secret access key. Create a new IAM user ,with all the permission policies needed .Use the asw configure command to configure the AWS cli

aws_access_key_id = YOUR_ACCESS_KEY_ID

aws_secret_access_key = YOUR_SECRET_ACCESS_KEY

default_region_name = YOUR_REGION_NAME

default_output_format = JSON

No alt text provided for this image
Configuring AWS cli

  • Creating an S3 bucket using CLI:

Once the CLI is set up, open your terminal or command prompt and run the following command to create an S3 bucket:

aws s3api create-bucket --bucket <your-bucket-name> --create-bucket-configuration LocationConstraint=<yiur-region>        
No alt text provided for this image
Creating an S3 bucket using AWS cli

Replace "<your-bucket-name>" with your desired bucket name and "<your-region>" with the AWS region where you want to create the bucket. You can choose any unique name for your bucket, but keep in mind that it must be globally unique across all existing bucket names.

  • Uploading objects using CLI:

To upload objects to your S3 bucket using the CLI, use the following command:

aws s3 cp <your-file-path> s3://<your-bucket-name>/        
No alt text provided for this image
Uploading a file in newly created S3 bucket

Replace "your-file-path>" with the local path of the file you want to upload and "<your-bucket-name>" with the name of your S3 bucket. This command will upload the file to the root of your bucket.

No alt text provided for this image
We can see 01.jpg uploaded in our newly created S3 bucket

Using API Method (Boto3)

  • Creating an S3 bucket using API:

If you prefer using the API instead of the CLI, you can create an S3 bucket programmatically. AWS provides SDKs for various programming languages to interact with their services. Here's an example using the Python SDK (boto3):

import boto3
s3 = boto3.client('s3')
s3.create_bucket(Bucket='<your-bucket-name>',
CreateBucketConfiguration={'LocationConstraint': '<your-region>'})        
No alt text provided for this image
Creating an S3 bucket using boto3

Replace "<your-bucket-name>" with your desired bucket name and "<your-region>" with the AWS region where you want to create the bucket.

No alt text provided for this image
Output for the S3 bucket created

  • Uploading objects using API:

To upload objects to your S3 bucket using the API, you can use the `upload_file` method provided by the S3 client in the SDK. Here's an example using the Python SDK (boto3):


s3.upload_file('<your-file-path>', '<your-bucket-name>', '<your-object-key>')        
No alt text provided for this image
Uploading a file in S3 bucket using boto3


Replace "<your-file-path>" with the local path of the file you want to upload, "<your-bucket-name>" with the name of your S3 bucket, and "<your-object-key>" with the desired key for the object in your bucket.

No alt text provided for this image
We can see 02.jpg uploaded in our newly created S3 bucket


Conclusion:

In this blog post, we explored two methods of creating an S3 bucket and uploading objects: using the Command Line Interface (CLI) and the Application Programming Interface (API). Both methods offer flexibility and convenience, allowing you to interact with S3 programmatically or through the command line. Whether you prefer the simplicity of the CLI or the power of the API, AWS provides comprehensive tools for managing your S3 storage efficiently.


To view or add a comment, sign in

Others also viewed

Explore content categories