0

我在 Python 3 上通过 django-storages boto 存储使用 S3 文件存储。当我尝试上传文件时,出现此错误:

boto.exception.S3ResponseError: S3ResponseError: 400 Bad Request
<?xml version="1.0" encoding="UTF-8"?>
<Error><Code>BadDigest</Code>
<Message>The Content-MD5 you specified did not match what we received.</Message>
...

我要保存的文件是一个随请求下载的文件。它的要点是:

import requests
from django.core.files.base import ContentFile

response = requests.get("http://example.com/some_file.pdf")
document_contents = ContentFile(response.text)
my_model.save("filename", document_contents)

我究竟做错了什么?

4

2 回答 2

0

请参阅此相关的 boto 问题:https ://github.com/boto/boto/issues/2868

Boto 在 Python3 中的字符串编码存在一些问题。如果您知道编码,则使用response.content而不是response.text解决问题:

document_contents = ContentFile(response.content)
于 2016-08-05T20:15:35.933 回答
0

我有一个类似的问题。

我改为 boto3 和存储引擎 to。

DEFAULT_FILE_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'

最后,我还必须使用 .encode('utf-8') 将内容转换为二进制

my_model.save("filename", document_contents.encode('uft-8'))
于 2016-11-23T09:35:45.350 回答