2014-11-07 20 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()爲我工作(我有同樣的錯誤)。

相關問題