存储在S3上的图像和缩略图的Django [英] Storing images and thumbnails on s3 in django

查看:549
本文介绍了存储在S3上的图像和缩略图的Django的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让我的图片缩略图,并使用Django的仓库,博托和SORL-缩略图存储在S3。我有工作,但它的速度很慢,即使有小的图像。我不介意它是缓慢的,当我保存表单并上传到S3图像,但我想它之后,迅速地显示图像。

I'm trying to get my images thumbnailed and stored on s3 using django-storages, boto, and sorl-thumbnail. I have it working, but it's very slow, even with small images. I don't mind it being slow when I save the form and upload the images to s3, but I'd like it to display the image quickly after that.

回答这个太问题解释说,缩略图不会被创建直到第一次访问,但您可以使用get_thumbnail()来预先创建它。

The answer to this SO question explains that the thumbnail won't be created until first access, but that you can use get_thumbnail() to create it beforehand.

<一个href="http://stackoverflow.com/questions/5562942/django-s3-boto-sorl-thumbnail-suggestions-for-optimisation">Django + S3(博托)+ SORL缩略图:建议优化

我做了,现在它上传图像时,而不是在显示时似乎所有条目到thumbnail_kvstore表中创建。

I'm doing that, and now it seems that all entries into the thumbnail_kvstore table are created when uploading the image, rather than when it is displayed.

问题是,显示图像的页面仍然很慢。综观记录面板在调试工具栏,它看起来像还有很多与S3通信。好像之后的图像和缩略图上载和缓存,网页应迅速呈现不与S3通信。

The problem is that the page displaying the image is still really slow. Looking at the logging panel in the debug toolbar, it looks like there is still lots of communication with s3. It seems like after the image and thumbnails are uploaded and cached, page should render quickly without communicating with s3.

我是什么做错了吗?谢谢!

What am I doing wrong? Thanks!

更新:弱黑客似乎已经得到了它的工作,但我很想知道如何做到这一点适当的:

Update: weak hack seems to have gotten it working, but I'd love to know how to do this properly:

<一个href="https://github.com/asciitaxi/sorl-thumbnail/commit/545cce3f5e719a91dd9cc21d78bb973b2211bbbf">https://github.com/asciitaxi/sorl-thumbnail/commit/545cce3f5e719a91dd9cc21d78bb973b2211bbbf

更新:为@sorl更多信息

Update: more information for @sorl

我正在使用2次:

添加视图:在此视图我提交表单创建模型在它的形象。图像被上传到S3。在post_save信号,我叫get_thumbnail()来生成缩略图需要它之前:

ADD VIEW: In this view I submit the form to create the model with the image in it. The image is uploaded to s3. In a post_save signal, I call get_thumbnail() to generate the thumbnail before it's needed:

im = get_thumbnail(instance.image, '360x360')

显示视图:在此视图中显示我在添加视图生成的缩略图:

DISPLAY VIEW: In this view I display the thumbnail generated in the add view:

    {% thumbnail object.image "360x360" as im %}
    <img src="{{ im.url }}" width="{{ im.width }}" height="{{ im.height }}">
    {% endthumbnail %}

如果没有补丁:

Without the patch:

添加视图:会在kvstore表3项,访问缓存的10倍(6套,4变),记录调试工具栏上的标签写着建立HTTP连接12次。

ADD VIEW: creates 3 entries in the kvstore table, accesses the cache 10 times (6 sets, 4 gets), logging tab of debug toolbar says "establishing HTTP connection" 12 times

显示视图:在kvstore表还只是3项,从缓存中仅有1弄,但调试工具栏上写着建立HTTP连接3次仍

DISPLAY VIEW: still just 3 entries in the kvstore table, just 1 get from cache, but debug toolbar says "establishing HTTP connection" 3 times still

由于只有线122上的变化:

With only the change on line 122:

添加视图:同上,除了日志只是说建立HTTP连接2倍 显示视图:同上,除了日志只是说建立HTTP连接1时间

ADD VIEW: same as above, except the logging only says "establishing HTTP connection" 2 times DISPLAY VIEW: same as above, except the logging only says "establishing HTTP connection" 1 time

还加入了变化对线路118:

Also adding the change on line 118:

添加视图:同上,但现在我们降到2建立HTTP连接消息 显示视图:同上,没有日志信息在所有

ADD VIEW: same as above, but now we are down to 2 "establishing HTTP connection" messages DISPLAY VIEW: same as above, with no logging messages at all

更新:它看起来像storage._setup()被调用两次,storage.url()被调用一次。根据时间安排,我会说,每个人让到S3连接:

UPDATE: It looks like storage._setup() is called twice, and storage.url() is called once. Based on the timing, I'd say each one makes connections to s3:

1304711315.4
_setup
1304711317.84
1304711317.84
_setup
1304711320.3
1304711320.39
_url
1304711323.66

这似乎在博托日志,它说建立HTTP连接3次得到体现。

This seems to be reflected by the boto logging, which says "establishing HTTP connection" 3 times.

推荐答案

由于SORL缩略图我对解决这个,如果它不能正常工作,因为我打算真正感兴趣的作者。如果键值sotre填充它目前存放:名称,存储和大小。我已取得的网址是基于名称,因此不会引起任何存储呼叫的假设。看着Django的仓库,<一个href="https://github.com/e-loue/django-storages/blob/master/storages/backends/s3boto.py#L214">https://github.com/e-loue/django-storages/blob/master/storages/backends/s3boto.py#L214这似乎是一个安全的假设作出。在你的补丁打了补丁出于某种原因read方法。当创建缩略图一个镜像文件实例从缓存中提取(如果没有创建它),然后当然呼叫,您可以阅读这将读取该文件,但用途是.URL它在存储与缓存的名字叫网址这inturn应是一个非存储访问操作。你可以尝试找出你的问题exacly,其中在code此存储访问happends?

As the author of sorl thumbnail I am really interested in solving this if it is not working as I intended. If the key value sotre is populated it will currently store: name, storage and size. I have made the assumption that the url is based on the name and thus should not cause any storage calls. Looking at django storages, https://github.com/e-loue/django-storages/blob/master/storages/backends/s3boto.py#L214 it seems like a safe assumption to make. In your patch you have patched the read method for some reason. When creating a thumbnail a ImageFile instance is fetched from cache (if not create it) then you can of course call read which will read the file, but the intended use is .url which calls url on the storage with the cached name which inturn should be a non storage access op. Could you try to isolate your problem to exacly where in your code this storage access happends?

另外,还要确保你有THUMBNAIL_DEBUG上,你有键值存储设置正确。

Also make sure you have THUMBNAIL_DEBUG on and that you have the key value store properly set up.

这篇关于存储在S3上的图像和缩略图的Django的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆