HomeToolsAbout a20k

Boto SDK

Creating a bucket in S3

import boto3 s3 = boto3.client('s3') s3.create_bucket(Bucket='bucket_name')

Read from S3 File

import boto3 s3 = boto3.resource('s3') object = s3.Object('bucket_name','key')

bucket_name is the name of the bucket key is the path to the file in the bucket.

s3.Object('bucket_name','key').get() s3.Object.get('bucket_name','key')

Write to S3 File

import boto3 s3 = boto3.resource('s3') client = boto3.client('s3') # using Object.put() object = s3.Object('bucket_name', 'my/key/fileName.txt') object.put(Body="new information") # using client.put_object() client.put_object(Body="write to file", Bucket='bucket_name', Key='my/key/fileName.txt')

Template

import boto3 import base64 from boto3 import client def lambda_handler(event, context): user_download_img ='Name Of Your Image in S3' print('user_download_img ==> ',user_download_img) # configure resource location s3 = boto3.resource('s3') bucket = s3.Bucket(u'Your-Bucket-Name') #pass your image Name to key obj = bucket.Object(key=user_download_img) # get Response response = obj.get() img = response[u'Body'].read() # Encoded the image to base64 myObj = [base64.b64encode(img)] # Printing the values print(type(myObj)) # base64 of image print(myObj[0]) # return variable return_json = str(myObj[0]) # repplace this 'b'' is must to get absoulate image. return_json = return_json.replace("b'","") encoded_image = return_json.replace("'","") return { 'status': 'True', 'statusCode': 200, 'message': 'Downloaded profile image', 'encoded_image':encoded_image # returning base64 of your image which in s3 bucket. }
© VincentVanKoh