Django存储S3-存储现有文件 [英] Django storages S3 - Store existing file
问题描述
我有带有最新django存储的django 1.11,并通过S3后端进行了设置.
I have django 1.11 with latest django-storages, setup with S3 backend.
我正在尝试以AWS图像链接为起点,以编程方式实例化ImageFile.在查看源代码/文档时,我不知道如何执行此操作.
I am trying to programatically instantiate an ImageFile, using the AWS image link as a starting point. I cannot figure out how to do this looking at the source / documentation.
我假设我需要创建一个文件,并为其提供从URL派生而没有域的路径,但是我找不到确切的方法.
I assume I need to create a file, and give it the path derived from the url without the domain, but I can't find exactly how.
此方法的最终目的是以编程方式创建w Image
对象,该对象指向S3图像(因此将新的ImageFile传递到图像的Imagefield).我拥有图像存储在其中的S3存储桶.
The final aim of this is to programatically create wagtail Image
objects, that point to S3 images (So pass the new ImageFile to the Imagefield of the image). I own the S3 bucket the images are stored in it.
上传图像可以正常工作,因此系统设置正确.
Uploading images works correctly, so the system is setup correctly.
更新
为澄清起见,我需要执行与正常过程相反的操作.通常,将物理图像提供给系统,然后系统创建ImageFile
,然后将文件上传到S3,并将URL分配给File.url
.我有File.url
,并且需要一个ImageFile
对象.
To clarify, I need to do the reverse of the normal process. Normally a physical image is given to the system, which then creates a ImageFile
, the file is then uploaded to S3, and a URL is assigned to the File.url
. I have the File.url
and need an ImageFile
object.
推荐答案
事实证明,在一些需要文件的模型中,使用DjangoStorages时,我要做的就是在文件字段中传递File
而不是,传递AWS S3对象密钥(而不是URL,仅传递对象密钥).
调用model.save()
时,将对S3进行boto
调用,以验证具有提供的键的对象是否存在,并保存该项目.
It turns out, in several models that expect files, when using DjangoStorages, all I had to do is instead of passing a File
on the file field, pass the AWS S3 object key (so not a URL, just the object key).
When model.save()
is called, a boto
call is made to S3 to verify an object with the provided key is there, and the item is saved.
这篇关于Django存储S3-存储现有文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!