我正在实现 Boto3 以将文件上传到 S3,一切正常。我正在做的过程如下:
我从 FileReader Javascript 对象获取 base64 图像。然后我通过 ajax 将 base64 发送到服务器,我解码 base64 图像并生成一个随机名称来重命名 key
参数
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')
这很好用,但考虑到性能,有没有更好的方法来改进它?
最佳答案
我用过这个,我相信它更有效和 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)
关于python - 使用 Boto3 使用 Python 将图像上传到 Amazon S3 的最有效方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43816346/