有没有可行的方法将动态生成的文件直接上传到亚马逊s3,而无需首先创建本地文件,然后上传到s3服务器?我使用python。谢谢
发布于 2015-06-12 01:18:00
下面是一个下载图片(使用requests库)并上传到s3,而不写入本地文件的示例:
import boto
from boto.s3.key import Key
import requests
#setup the bucket
c = boto.connect_s3(your_s3_key, your_s3_key_secret)
b = c.get_bucket(bucket, validate=False)
#download the file
url = "http://en.wikipedia.org/static/images/project-logos/enwiki.png"
r = requests.get(url)
if r.status_code == 200:
#upload the file
k = Key(b)
k.key = "image1.png"
k.content_type = r.headers['content-type']
k.set_contents_from_string(r.content)
发布于 2014-08-09 09:31:16
您可以使用Python标准库中的BytesIO。
from io import BytesIO
bytesIO = BytesIO()
bytesIO.write('whee')
bytesIO.seek(0)
s3_file.set_contents_from_file(bytesIO)
发布于 2012-09-25 02:17:13
我假设您使用的是boto
。boto
的Bucket.set_contents_from_file()
将接受StringIO
对象,并且您编写的任何用于将数据写入文件的代码都应易于修改以写入StringIO
对象。或者,如果您生成一个字符串,则可以使用set_contents_from_string()
。
https://stackoverflow.com/questions/12570465
复制相似问题