【问题标题】:Django Storages Boto Bad DigestDjango 存储 Boto 坏文摘
【发布时间】:2016-08-05 20:15:35
【问题描述】:

我在 Python 3 上通过 django-storages boto storage 使用 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)

我做错了什么?

【问题讨论】:

    标签: python django python-3.x amazon-s3 python-django-storages


    【解决方案1】:

    查看这个相关的 boto 问题:https://github.com/boto/boto/issues/2868

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

    document_contents = ContentFile(response.content)
    

    【讨论】:

      【解决方案2】:

      我也遇到过类似的问题。

      我改成了boto3,存储引擎改成了to。

      DEFAULT_FILE_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'
      

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

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

      【讨论】:

        猜你喜欢
        • 2016-12-20
        • 1970-01-01
        • 2017-06-20
        • 1970-01-01
        • 1970-01-01
        • 2014-02-10
        • 2012-10-12
        • 2018-08-25
        • 2018-07-16
        相关资源
        最近更新 更多