I'm using S3 file storage through django-storages boto storage on Python 3. When I try to upload a file, I get this error:
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>
...
The file I am trying to save is a file being downloaded with requests. The gist of it is:
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)
What am I doing wrong?
See this relevant boto issue: https://github.com/boto/boto/issues/2868
Boto has some problems with string encodings in Python3. If you know the encoding, you Using response.content
instead of response.text
fixes the problem:
document_contents = ContentFile(response.content)