使用Python添加元数据以将图像上传到S3
我已成功将图像添加到 S3 上的存储桶,但问题是我不确定如何将内容类型设置为“image/png”。这是我的代码
image = Image.open(self.image)
conn = S3Connection(settings.AWS_ACCESS_KEY_ID, settings.AWS_SECRET_ACCESS_KEY)
out_im2 = cStringIO.StringIO()
image.save(out_im2, 'PNG')
b = conn.get_bucket('new_test_bucket')
k = b.new_key(self.title+'.png')
k.set_contents_from_filename(str(self.image))
,目前它正在作为“application/octet-stream”上传。
I'm successfully adding an image to a bucket on S3, but the problem is I'm not sure how to set the content-type to 'image/png'. Here is my code
image = Image.open(self.image)
conn = S3Connection(settings.AWS_ACCESS_KEY_ID, settings.AWS_SECRET_ACCESS_KEY)
out_im2 = cStringIO.StringIO()
image.save(out_im2, 'PNG')
b = conn.get_bucket('new_test_bucket')
k = b.new_key(self.title+'.png')
k.set_contents_from_filename(str(self.image))
Currently it is being uploaded as 'application/octet-stream'.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这就是我的图像上传代码(使用 boto)的工作原理:
至少是其中的一部分。
This is how my image upload code (using boto) works:
Well at least part of it.
我发现在
set_contents_from_file
之前设置元数据对我来说很有效。不过我正在使用 GCS,所以也许情况有所不同。I found that setting the metadata before you
set_contents_from_file
works for me. I am using GCS though, so maybe that's different..