Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Most efficient way to upload image to Amazon S3 with Python using Boto3

Tags:

python

boto3

I'm implementing Boto3 to upload files to S3, and all works fine. The process that I'm doing is the following:

I get base64 image from FileReader Javascript object. Then I send the base64 by ajax to the server, I decode the base64 image and I generate a random name to rename the key argument

data = json.loads(message['text'])
dec = base64.b64decode(data['image'])
s3 = boto3.resource('s3')
s3.Bucket('bucket_name').put_object(Key='random_generated_name.png', Body=dec,ContentType='image/png',ACL='public-read')

This works fine but respect to performance, is there a better way to improve it?

like image 816
dfrojas Avatar asked May 06 '17 03:05

dfrojas


1 Answers

I used this and I believe its more effective and pythonic.

    import boto3
    s3 = boto3.client('s3')
    bucket = 'your-bucket-name'
    file_name = 'location-of-your-file'
    key_name = 'name-of-file-in-s3'
    s3.upload_file(file_name, bucket, key_name)
like image 179
medobills Avatar answered Oct 16 '22 23:10

medobills