2014-11-07 42 views
6

我试图将文件上传到boto为什么在上传boto文件时会得到400?

import io 

from boto.s3 import connection 
from boto.s3 import key 

conn = connection.S3Connection() 
bucket = conn.get_bucket('my-bucket') 

my_key = key.Key(bucket, 'asdf') 

d = b'this is a test....\n' * 512000 
f = io.BytesIO(d) 

my_key.send_file(f, size=4*1024) 

然而,这会导致:

boto.exception.S3ResponseError: S3ResponseError: 400 Bad Request 
<?xml version="1.0" encoding="UTF-8"?><Error><Code>BadRequest</Code><Message>An error occurred when parsing the HTTP request.</Message><RequestId>[hex request ID]</RequestId><HostId>[giant piece of base64]</HostId></Error> 

为什么这个请求会失败?

(注:我使用send_file这里的整个原因是因为open显然只支持读取...)

+0

如果您执行''my_key.set_contents_from_file(f)'',会发生什么情况。你有同样的错误吗? – garnaat 2014-11-07 16:54:13

+0

@garnaat调用成功;这一点的一部分是确保'boto'不会将整个文件读入内存:如果添加size参数,'set_contents_from_file'只会读取第一个'size'字节。 (这是文档所说的功能,但不是我想要的。) – Thanatos 2014-11-07 21:26:51

+0

这听起来像你真正想要的是真正的流式传输,但不幸S3不支持这一点。至少还没有。 – garnaat 2014-11-07 21:42:28

回答

4

对于它的价值,与set_contents_from_file()更换send_file()为我工作(我有同样的错误)。

相关问题