Upload and Fetch Image from Amazon S3 Bucket in Python

Posted By :Saurabh Gupta |31st August 2020

Introduction:-  Amazon Web Services (AWS) has become a leader in cloud computing. One of its core components is the S3 service, the object storage service offered by AWS. Because of its impressive speed, availability and durability, it is becoming the new way to store images, videos, data, and files. To build scalable applications you will need to combine and integrate S3 with other services. It was launched in 2006 when no company had the cloud computing business model. Today 70% of the cloud computing market is depending on AWS.


Prerequisites:-  


To configure Odoo with Amazon S3 storage you will need to have the following:

1:- An AWS account

2:- Amazon S3 storage service subscription

3:- Create a bucket in the S3 storage to store the file data reference Link

4:-  Boto3 is the name of the Python SDK for AWS. It allows you to directly CREATE, UPDATE, and DELETE S3 Objects in  AWS resources from your Python scripts.

Implementation:-

Here is the Python implementation.

1)  How to upload a file in S3 Bucket using Boto3?

            import boto3

            S3_KEY = 'AMAZON_S3_KEY'            

            S3_SECRET = 'AMAZON_S3_SECRET_KEY'            

            S3_BUCKET = 'BUCKET_NAME'            

            S3_FOLDER = 'DIRECTORY_NAME'              # which you want to create an S3 Bucket            

            S3_REGION = 'S3_REGION'                   # S3 Region name like ap-south-1, ap-north-1 etc            

            FILENAME = 'upload.jpg'                   # S3 obj filename            

            FILE_PATH = '/temp/upload.jpg'          -# local directory file path

            s3_client = boto3.client('s3', aws_access_key_id=S3_KEY, aws_secret_access_key=S3_SECRET)            

            s3_client.upload_file( Bucket = S3_BUCKET,  Filename = FILENAME , Key = FILE_PATH)
            


2) How to fetch a file from S3 Bucket using Boto3?

            s3_client = boto3.client('s3', aws_access_key_id=S3_KEY, aws_secret_access_key=S3_SECRET)

            fileobj = s3_client.get_object(Bucket = S3_BUCKET, Key = FILENAME )

for more Boto3 python SDK Link

All Done!!

The above used variables for Access Key ID and Secret Access Key are getting from AWS Panel  while creating the Amazon S3 Bucket .

 


About Author

Saurabh Gupta

Saurabh has 3.5 years of experience in Python and open erp also known as 'Odoo'. He has worked on JavaScript, Jquery, Backbone.JS, SQL Database , He loves to play Football.

Request For Proposal

[contact-form-7 404 "Not Found"]

Ready to innovate ? Let's get in touch

Chat With Us