Zags
Zags

Reputation: 41338

Django Storages Boto Bad Digest

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?

Upvotes: 0

Views: 705

Answers (2)

mithuntnt
mithuntnt

Reputation: 517

I had a similar problem.

I changed to boto3 and storage engine to to.

DEFAULT_FILE_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'

Finally I also had to convert the content to binary using .encode('utf-8')

my_model.save("filename", document_contents.encode('uft-8'))

Upvotes: 0

Zags
Zags

Reputation: 41338

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)

Upvotes: 1

Related Questions