Hello Readers, I am here with another blog. As we learned new concepts always. In this blog, We are going to learn how to perform operations on the AWS s3 bucket using boto3.
First of all, We need to know about boto. What is boto?? Boto3 is the Amazon Web Services (AWS) Software Development Kit (SDK) for Python, which allows Python developers to write software that makes use of services like Amazon S3 and Amazon EC2. You can find the latest, most up-to-date, documentation at our doc site, including a list of services that are supported.Boto3 is maintained and published by AWS.
The installation process of Boto3 is very easy and simple. You can follow below:
- We must have python in the system.
- You should run the below command to install boto3
pip3 install boto3 python -m pip install boto3
- To verify the boto3 installation, run the below command :
pip3 show boto3
AWS S3 bucket
Amazon Simple Storage Service (Amazon S3) is an object storage service that offers industry-leading scalability, data availability, security, and performance. Customers of all sizes and industries can use Amazon S3 to store and protect any amount of data for a range of use cases, such as data lakes, websites, mobile applications, backup and restore, archive, enterprise applications, IoT devices, and big data analytics. Amazon S3 provides management features so that you can optimize, organize, and configure access to your data to meet your specific business, organizational, and compliance requirements. If you want to know more then follow this link.
First of all, We should have an s3 bucket where we want to perform these tasks. You can follow my last blog to create an s3 bucket using boto3.
Upload file to s3 bucket using boto3
Now, We will write the code for it. Simply, We need to import boto3, and then we need to define the resource which we want to create or perform the task.
import boto3 s3_client = boto3.client('s3') response = s3_client.upload_file('/home/knoldus/Desktop/knoldus/learning/boto/sample.py' ,'demo-blog-12','sam.py') print(response)
Now, You can verify the operation via the AWS console.
List the files in the s3 bucket
Now, We will write code for listing the file of the s3 bucket.Here we are using list_objects_v2() function.
import boto3 client = boto3.client('s3') response = client.list_objects_v2(Bucket='demo-blog-12', ) for key in (response['Contents']): print(key)
Now, You can see in the below picture.
Read the file from the s3 bucket
In this operation, We will import boto3 and use the get_object() function.
import boto3 import json s3_client = boto3.client('s3') response = s3_client.get_object(Bucket='demo-blog-12',Key="newfile") data = response['Body'].read() print(data)
You can see in the below picture:
Delete files from the s3 bucket
In this operation, We will import boto3 and use delete_object() function.
import boto3 client = boto3.client('s3') response = client.delete_object(Bucket='demo-blog-12',Key='newfile') print(response)
In this blog, We have performed a few operations on the s3 bucket. If you find this blog helpful then please like and share it with your friends.