python [将图像上传到s3存储桶]

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python [将图像上传到s3存储桶]相关的知识,希望对你有一定的参考价值。

import boto3	# Amazon's aws library for python 3
import numpy as np
import matplotlib.pyplot as plt

# create a connection to s3
s3 = boto3.resource('s3', 
                    aws_access_key_id=aws_access_key_id, 
                    aws_secret_access_key=aws_secret_access_key)

# you need a bucket, make one and put its name here
bucket = "some_bucket_name"       

# lets plot something so we have an image to upload
plt.plot([1,2,3,4])
plt.ylabel('some numbers')

# save the plot to a static folder
image_name = "test.png"          
plt.savefig("static/" + image_name)

# upload image to aws s3
# warning, the ACL here is set to public-read
img_data = open("static/" + image_name, "rb")
s3.Bucket(bucket).put_object(Key=image_name, Body=img_data, 
                                 ContentType="image/png", ACL="public-read")

 # Generate the URL to get 'key-name' from 'bucket-name'
 url = "http://" + bucket + ".s3.amazonaws.com/" + image_name

# this url should open the uploaded image as an image in a browser window
        

以上是关于python [将图像上传到s3存储桶]的主要内容,如果未能解决你的问题,请参考以下文章

如何将图像上传到 s3 存储桶

从 node.js 将图像上传到 s3 存储桶

如何在 Android 中使用 REST API 将图像上传到 S3 存储桶,出现禁止错误:403

使用 AlamoFire 和预签名 URL 将图像上传到 S3 存储桶时出现问题

将图像从库上传到s3存储桶 - 创建文件对象?

代号一上传图片到S3存储桶权限