[英]Errno 22 invalid mode ('rb') or filename - django custom file storage
I have overwritten the default file storage in django to save the model image fields in amazon s3. 我已经覆盖了django中的默认文件存储,以将模型图像字段保存在Amazon s3中。
only the open
method is not working as expected, I am getting: 只有open
方法无法按预期工作,我得到:
[Errno 22] invalid mode ('rb') or filename: http://image.buzzhearts.com/media/2015/11/12/11/99f0301c46d7945c91b16b0c2d61ab13.jpg
the file is already in s3: http://image.buzzhearts.com/media/2015/11/12/11/99f0301c46d7945c91b16b0c2d61ab13.jpg 该文件已经在s3中: http : //image.buzzhearts.com/media/2015/11/12/11/99f0301c46d7945c91b16b0c2d61ab13.jpg
code: 码:
class CS3Storage(FileSystemStorage):
def open(self, name, mode='rb'):
return File(open(self.path(name), mode)) # <-- problem
def path(self, name):
# returns http://image.buzzhearts.com/media/2015/11/12/11/99f0301c46d7945c91b16b0c2d61ab13.jpg
return 'http://image.buzzhearts.com/%s' % name
what am I missing? 我想念什么? I think, I have to first get the image file from s3 with eg requests
and then open right? 我想,我必须先从s3中获取带有例如requests
的图像文件,然后打开对不对?
open()
takes a filename, it does not support urls. open()
使用文件名,它不支持url。
If you want to fetch the url, you need to use a library like requests
. 如果您想获取网址,则需要使用一个类似于requests
的库。 Since the file is on S3, you could use a tool like boto. 由于文件位于S3上,因此可以使用boto之类的工具。
Instead of writing your own file storage, you might find it easier to use django-storages-redux . 与其编写自己的文件存储, 不如使用django-storages-redux更容易。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.