使用S3.Object()。put()在boto3 1.5.36中存储matp中的matplotlib图像 [英] Storing matplotlib images in S3 with S3.Object().put() on boto3 1.5.36

查看:675
本文介绍了使用S3.Object()。put()在boto3 1.5.36中存储matp中的matplotlib图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 matplotlib 绘制图表,我希望立即存储为S3对象。

Amongst other things I am drawing plots using matplotlib, which I would like to immediately store as S3 objects.

根据这另一个问题和罚款手册,我需要 S3.Object.put()将我的数据移到AWS中,程序应该是

According to the answers provided in this and this other question and the fine manual, I need S3.Object.put() to move my data into AWS and the procedure should be along the lines of

from matplotlib import pyplot as plt
import numpy as np
import boto3
import io

# plot something
fig, ax = plt.subplots()
x = np.linspace(0, 3*np.pi, 500)
a = ax.plot(x, np.sin(x**2))

# get image data, cf. https://stackoverflow.com/a/45099838/1129682
buf = io.BytesIO()
fig.savefig(buf, format="png")
buf.seek(0)
image = buf.read()

# put the image into S3
s3 = boto3.resource('s3', aws_access_key_id=awskey, aws_secret_access_key=awssecret)
s3.Object(mybucket, mykey).put(ACL='public-read', Body=image)

但是,我最终得到一个内容长度为零的新S3对象。

However, I end up with a new S3 object with content-length zero.

以下为我提供了一个内容长度为6的新S3对象。

The following gives me a new S3 object with content-length 6.

s3.Object(mybucket, mykey).put(ACL='public-read', Body="foobar")

当我放下一行时,我最终得到了S3对象中的内容,但它不是一个可用的图像:

When I put the next line, I end up with content in the S3 object, but its not a usable image:

s3.Object(mybucket, mykey).put(ACL='public-read', Body=str(image))

我可以通过查看实际文件来使其工作,如下所示:

I can make it work by going through an actual file, like this:

with open("/tmp/iamstupid","wb") as fh:
    fig.savefile(fh, format="png")
s3.Bucket(mybucket).upload_file("/tmp/iamstupid", mykey)

所以它似乎有效。我只是无法正确使用界面。我究竟做错了什么?如何使用 S3.Object.put()

So it seems to work. I am just unable to use the interface correctly. What am I doing wrong? How can I achieve my goal using S3.Object.put()

推荐答案

<实现我的目标p>我能够解决它。我在这个问题中找到了答案。它是一个Python3的东西。根据我的理解,Python3通常与unicode一起使用。如果你想要单个字节,你必须明确它。因此,正确的用法是

I was able to resolve it. I found the answer in this question. Its a Python3 thing. From what I understand Python3 "usually" works with unicode. If you want single bytes you have to be explicit about it. The correct usage therefore is

s3.Object(mybucket, mykey).put(ACL='public-read', Body=bytes(image))

我发现这有点奇怪,因为 buf。 read()已经应该返回一个 bytes 类型的对象,但我确实停止想知道,b / c现在可以正常工作。

I find this a bit strange, since buf.read() is already supposed to return an object of type bytes but I did stop wondering, b/c it works now.

这篇关于使用S3.Object()。put()在boto3 1.5.36中存储matp中的matplotlib图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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