简体   繁体   English

Errno 22无效模式('rb')或文件名-Django自定义文件存储

[英]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.

相关问题 Python ElementTree XML IOError:[Errno 22]无效模式(&#39;rb&#39;)或文件名 - Python ElementTree XML IOError: [Errno 22] invalid mode ('rb') or filename Errno 22:使用pyinstaller运行spec文件时无效模式(&#39;rb&#39;)或文件名:&#39;&#39; - Errno 22:invalid mode('rb') or filename:' ' while running a spec file while using pyinstaller IOError:[Errno 22]无效模式(&#39;rb&#39;)或文件名:&#39;\\ xe2 \\ x80 \\ xaaE: - IOError: [Errno 22] invalid mode ('rb') or filename: '\xe2\x80\xaaE: IOError:[Errno 22]无效模式(&#39;rb&#39;)或文件名:&#39;\\ x89PNG \\ n&#39; - IOError: [Errno 22] invalid mode ('rb') or filename: '\x89PNG\n' Python etree.ElementTree IOError:[错误22]无效模式(&#39;rb&#39;)或文件名- - Python etree.ElementTree IOError: [Errno 22] invalid mode ('rb') or filename - IOError:[Errno 22]无效模式(&#39;r&#39;)或文件名 - IOError: [Errno 22] invalid mode ('r') or filename IOError:[Errno 22]无效模式('w')或文件名 - IOError: [Errno 22] invalid mode ('w') or filename Errno 22无效模式w +或文件名 - Errno 22 invalid mode w+ or filename IOError:[Errno 22]无效模式(&#39;wb&#39;)或文件名: - IOError: [Errno 22] invalid mode ('wb') or filename: 使用python打开CSV文件(IOError:[Errno 22]无效模式(&#39;r&#39;)或文件名:“ [&#39;C:\\\\\\\\ bigdata) - Open csv file using python(IOError: [Errno 22] invalid mode ('r') or filename: "['C:\\\\bigdata)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM