【问题标题】:How to download Django media file uploaded using Django storages?如何下载使用 Django 存储上传的 Django 媒体文件?
【发布时间】:2017-03-10 08:25:55
【问题描述】:

我已使用 Django-storages 成功地将 Word 文档存储到 S3。

class Document(TitleSlugDescriptionModel, TimeStampedModel):
    document = models.FileField(upload_to=user_directory_path)

现在在 celery 任务中,我需要再次下载此文件以便在工作人员中进行进一步处理。

我需要从 URL 读取文件,然后显式创建本地副本,还是有什么方法可以使用 Django-storages 创建本地副本?

【问题讨论】:

    标签: django python-django-storages


    【解决方案1】:

    您可以在 Django 中使用 read 方法直接读取文件,如 document.read() 这将输出二进制文件,您可以使用

    将其保存到文件中
    f=open(filename, 'wb')
    f.write(document.read())
    f.close
    

    您也可以在 S3 上生成文件的 URL。文档中的更多信息:https://django-storages.readthedocs.io/en/latest/backends/amazon-S3.html

    【讨论】:

      猜你喜欢
      • 2020-02-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-14
      • 1970-01-01
      • 1970-01-01
      • 2016-02-21
      • 2014-10-20
      相关资源
      最近更新 更多