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

ぃ、小莉子 提交于 2019-12-24 00:45:55

问题


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?


回答1:


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)


来源:https://stackoverflow.com/questions/43816346/most-efficient-way-to-upload-image-to-amazon-s3-with-python-using-boto3

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!