Amazon Simple Storage Service (S3) is a powerful and scalable object storage service offered by Amazon Web Services (AWS). It allows developers to store and retrieve any amount of data at any time. To interact with S3 programmatically, AWS provides the Boto3 SDK for Python, which simplifies the process of integrating your applications with S3.
In this blog post, we'll explore the basic operations you can perform with S3 using the Boto3 SDK. We'll cover creating an S3 bucket, deleting a bucket, uploading a file to a bucket, and downloading a file from a bucket.
Prerequisites
Before diving into the code, make sure you have the following prerequisites:
AWS Account: You need an AWS account with appropriate permissions to create and manage S3 resources.
AWS Access Key and Secret Key: Obtain your AWS access key and secret key from the AWS Management Console.
Boto3 Installation: Install the Boto3 library using the following command:
pip install boto3
Setting Up Boto3
To start using Boto3, you need to configure your AWS credentials. Create a new Python script and configure Boto3 with your AWS credentials or You can configure the AWS CLI with below given details.
Configuring the AWS Command Line Interface (CLI) is a crucial step for interacting with AWS services through the command line. The AWS CLI provides a unified tool to manage your AWS resources, and configuring it involves setting up your AWS credentials and default region. Here are the steps to configure the AWS CLI
Step 1: Install AWS CLI
Make sure you have the AWS CLI installed on your machine. You can download and install it from the official AWS CLI website.
Step 2: Open a Terminal or Command Prompt
Open a terminal or command prompt on your machine. The process might vary depending on your operating system.
Step 3: Run aws configure
In the terminal or command prompt, run the following command:
aws configure
Step 4: Enter AWS Access Key ID
You will be prompted to enter your AWS Access Key ID. This is a unique identifier associated with your AWS account. You can obtain this key from the AWS Management Console.
AWS Access Key ID [None]: YOUR_ACCESS_KEY_ID
Step 5: Enter AWS Secret Access Key
Next, you'll be prompted to enter your AWS Secret Access Key. This key is used to authenticate your access to AWS services.
AWS Secret Access Key [None]: YOUR_SECRET_ACCESS_KEY
Now we will run our Code one by one with various function:
1: Show the bucket list
This code will list down how many bucket is already created.
After Running the script you can see that there is no output because there is no bucket available.
Will create and test again the same.
No we have created the Test Bucket let see by script it will show up or not.
This code will create a New bucket.
And we have to uncomment the call function.
As you can see there is no bucket with name testbucketboto23 now i will run the code again.
After executing it created the Bucket
2:Upload the file from local to S3 bucket.
Again we will comment the create bucket function and uncomment the upload file function and run the code to upload the file from local to S3 bucket
Uncomment this call and function.
After running the script you can see it got uploaded
3: Download the same file from S3 bucket to local.
For that we will delete the file from our local system.
Now we will uncomment the download call and function
There is no file available now lets run the script.
4: Delete the file from S3 bucket.
For that we have to uncomment the function as shown below.
You can see the file has been run successfully lets see over the bucket the file got deleted or not.
5: Delete the bucket
Now we will uncomment the delete call and function
After executing the code the bucket got deleted make sure you have empty the bucket before deleting you can also write the function to empty the bucket.
Thank You, please share if this articles finds you insightful.