使用Boto3使用Python将图像上传到Amazon S3的最有效方法 [英] Most efficient way to upload image to Amazon S3 with Python using Boto3

查看:415
本文介绍了使用Boto3使用Python将图像上传到Amazon S3的最有效方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在实现Boto3将文件上传到S3,并且一切正常.我正在执行的过程如下:

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

我从FileReader Javascript对象获取base64图像.然后,我通过ajax将base64发送到服务器,对base64图像进行解码,并生成一个随机名称以重命名key参数

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?

推荐答案

我使用了它,并且我相信它更有效,更pythonic.

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)

这篇关于使用Boto3使用Python将图像上传到Amazon S3的最有效方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆