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
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
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>
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.
To upload objects to your S3 bucket using the CLI, use the following command:
aws s3 cp <your-file-path> s3://<your-bucket-name>/
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.
Recommended by LinkedIn
Using API Method (Boto3)
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>'})
Replace "<your-bucket-name>" with your desired bucket name and "<your-region>" with the AWS region where you want to create the bucket.
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>')
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.
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.