Django storages S3 - Store existing file

谁说胖子不能爱 提交于 2020-01-24 14:12:48

问题


I have django 1.11 with latest django-storages, setup with S3 backend.

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.

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.

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.

Update

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.


回答1:


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.



来源:https://stackoverflow.com/questions/50609686/django-storages-s3-store-existing-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!