我认为 Kekito 是对的,您不能直接 POST 到 upload_url。
但是,如果您想测试 BlobstoreUploadHandler,您可以通过以下方式伪造它通常会从 Blobstore 收到的 POST 请求。假设您的处理程序位于 /handler :
import email
...
def test_upload(self):
blob_key = 'abcd'
# The blobstore upload handler receives a multipart form request
# containing uploaded files. But instead of containing the actual
# content, the files contain an 'email' message that has some meta
# information about the file. They also contain a blob-key that is
# the key to get the blob from the blobstore
# see blobstore._get_upload_content
m = email.message.Message()
m.add_header('Content-Type', 'image/png')
m.add_header('Content-Length', '100')
m.add_header('X-AppEngine-Upload-Creation', '2014-03-02 23:04:05.123456')
# This needs to be valie base64 encoded
m.add_header('content-md5', 'd74682ee47c3fffd5dcd749f840fcdd4')
payload = m.as_string()
# The blob-key in the Content-type is important
params = [('file', webtest.forms.Upload('test.png', payload,
'image/png; blob-key='+blob_key))]
self.testapp.post('/handler', params, content_type='blob-key')
我通过深入研究 blobstore 代码发现了这一点。重要的是,blobstore 发送到 UploadHandler 的 POST 请求不包含文件内容。相反,它包含一个“电子邮件消息”(嗯,像电子邮件一样编码的信息),其中包含有关文件的元数据(内容类型、内容长度、上传时间和 md5)。它还包含一个 blob-key,可用于从 blobstore 中检索文件。