3

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

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

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

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

4

1 回答 1

0

您可以使用 read 方法直接在 Django 中读取文件,因为 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-09T03:06:21.613 回答