使用Boto将Matplotlib图像直接保存到S3,而不保存在本地

5 投票
1 回答
2406 浏览
提问于 2025-04-18 18:13

你能直接把一个图表保存到s3上,而不需要先在本地保存文件吗?

from boto.s3.connection import S3Connection
from boto.s3.key import Key

k = Key(bucket)
k.key = "mykey"

plt.savefig(k.key) //??

1 个回答

0
BUCKET_NAME = 'enter your bucket name'
KEY = 'enter full path where to store the image'

df = pd.read_csv('./gene_expression.csv')
df.hist(by='Cancer Present', figsize=[12, 8], bins=15)
img_data = io.BytesIO()
plt.savefig(img_data, format='png')
img_data.seek(0)

s3 = boto3.resource('s3')
bucket = s3.Bucket(BUCKET_NAME)
bucket.put_object(Body=img_data, ContentType='image/png', Key=KEY)

当然可以!请把你想要翻译的内容发给我,我会帮你用简单易懂的语言解释清楚。

撰写回答