Create an S3 bucket and upload objects using both the Command Line Interface (CLI) and Application Programming Interface (API) methods.
Amazon S3 (Simple Storage Service) is a versatile cloud storage platform provided by Amazon Web Services (AWS). It allows users to store, manage, and retrieve data in a scalable and secure manner. In this blog post, we'll explore two straightforward methods for creating S3 buckets and uploading objects: using the Command Line Interface (CLI) and the Application Programming Interface (API). Understanding these methods will empower you to efficiently manage your data and seamlessly integrate S3 with your applications.
Method 1: Command Line Interface (CLI):
Step 1: Install and Configure AWS CLI:
Download and install the AWS CLI on your computer. After installation, use the 'aws configure' command to provide your AWS access credentials.
Step 2: Create an S3 Bucket:
Using the 'aws s3api' command, create an S3 bucket with a unique name. Choose the region where you want the bucket to reside.
aws s3api create-bucket --bucket my-bucket-name --region us-west-2 --create-bucket-configuration LocationConstraint=us-west-2
Step 3: Upload Objects:
With the 'aws s3' command, upload objects to the bucket. For instance, to upload a file named "example.txt" to the root of the bucket:
aws s3 cp C:\Users\Lenovo\Pictures\pic.jpeg s3://sasibuc/
Method 2: Application Programming Interface (API):
Step 1: Set Up AWS SDK:
Select the AWS SDK for your preferred programming language (e.g., Python, JavaScript, Java) and install it in your development environment. Configure the SDK with your AWS access credentials.
Step 2: Create an S3 Bucket:
Using the SDK, create an S3 bucket by calling the appropriate method with the desired bucket name and region:
import boto3
Recommended by LinkedIn
AWS_REGION = "ap-south-1"
client = boto3.client("s3", region_name=AWS_REGION)
bucket_name = "boto.buc"
location = {'LocationConstraint': AWS_REGION}
response = client.create_bucket(Bucket=bucket_name, CreateBucketConfiguration=location)
print("Amazon S3 bucket has been created")
Step 3: Upload Objects:
To upload objects, utilize the SDK's upload method and specify the bucket name, object key (path within the bucket), and the file to upload:
import boto3
s3_client = boto3.client('s3')
s3_client.upload_file(
Filename='C:/Users/Lenovo/Pictures/IMG_20230129_173559.jpg',
Bucket='boto.buc',
Key='boto/IMG_20230129_173559.jpg'
)
Conclusion:
Both the Command Line Interface (CLI) and the Application Programming Interface (API) provide straightforward methods to create S3 buckets and upload objects. The CLI is ideal for quick tasks and scripting, while the API caters to developers seeking programmatic control over S3 within their applications.
With these two methods at your disposal, you can harness Amazon S3's impressive capabilities, including scalability, security, and reliability, for efficient data management in the cloud. Whether you prefer the simplicity of the CLI or the flexibility of the API, Amazon S3 remains an invaluable resource for modern cloud storage needs.