我想继续问昨天发布的问题。我想将一些视频剪辑上传到 amazon s3。这些视频是作为中间结果生成的。所以我更喜欢将这些小视频片段存储在内存中(大约 400~500 KB),然后将每个片段上传到 s3。上传后,临时文件可以在内存中删除。因此,我想使用 tempfile。但是下面的代码有错误。请看一下,如何正确执行?
@contextmanager
def s3upload(key):
with tempfile.SpooledTemporaryFile(max_size=1021*1000) as buffer:
yield buffer
buffer.seek(0)
# key.send_file(buffer)
k.set_contents_from_file(buffer)
k.set_acl('public-read')
conn = boto.connect_s3()
b = conn.get_bucket('cc_test_s3')
k = Key(b)
k.key = '1.flv'
mime = mimetypes.guess_type('1.flv')[0]
with s3upload(k) as out:
out.write('1.flv')
输出:
The size of file uploaded is 5 KB, which is much less than the actual size of 1.flv (~400 KB).