1
我想继续提问昨天发布的问题。我想上传一些视频剪辑到亚马逊S3。这些视频是作为中间结果生成的。所以我更喜欢将这些小视频剪辑存储在内存中(大约400〜500 KB),然后将其上传到s3。上传后,临时文件可以在内存中删除。因此,我想使用tempfile。但是在下面的代码中有错误。请看看以及如何正确地做到这一点?将存储在内存中的文件上传到s3
@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).
我在/ tmp目录下创建一个临时文件。然后将中间结果写入此临时文件。文件上传后,该临时文件被删除。 – susanne
好的,所以在s3fuse中,您的文档将仅占用/存储在s3上。你只会感觉它像本地目录(/ tmp)。您不需要删除临时文件。 –