我如何使用Django,SORL-缩略图,memcached的,和S3在一起呢?如何使用、缩略图、Django、SORL

2023-09-11 09:17:28 作者:处处留意你的讯息

我有一个项目中,我要开始创建用户上传的图片,其中previously我一直在产生一些具体尺寸,那些动态调整大小的缩略图。我有现成的模式,从而重新presents看起来像这样的图片:

I've got a project in which I need to start creating dynamically-resized thumbnails of user-uploaded images, where previously I'd been generating some specifically sized ones. I have an existing model, which represents an image which looks like this:

class Image(models.Model):
  original = models.URLField()
  small = models.URLField()
  medium = models.URLField()
  large = models.URLField()

每个那些的填充有一个URL到S3的各自的图像。在上传的时候,所有的版本的创建(实际上后不久,通过计划任务),存储在S3中,然后在模型上设置。展望未来,我们的设计需要许多不同的尺寸为每个缩略图,所以将它们存储所有在db是不实际的。我想只保留并做其余的在模板上即时。

Each of those is filled in with a URL to the respective image on S3. At the time of upload, all the versions are created (actually shortly after, via a scheduled task), stored in S3, and then set on the model. Going forward, our design needs many different sizes for each thumbnail, so storing them all in the db is not practical. I'd like to just keep original and do the rest on-the-fly in the templates.

因此​​,这是我来 SORL-缩略图。这似乎是打算做什么,我想,但我有点迷失,如何它应该是工作(这不是现在)。作为一个测试,我已经采取了一个基本观点/模板,列出了一些照片,并试图缩略图他们,很像教程:

So this is where I come to sorl-thumbnail. It seems to be intended to do what I want, but I'm kind of lost as to how it's supposed to be working (it's not at the moment). As a test I've taken made a basic view/template which lists some images and attempts to thumbnail them, much like the tutorial:

{% for img in image_set %}
  {% thumbnail img.original "180x180" crop="center" format="PNG" as im %}
  <img src="{{ im.url }}" width="{{ im.width }}" height="{{ im.height }}">
  {% endthumbnail %}
{% endfor %}

这是最终生成的URL像缓存/ 84 / 8F / 848fb078f2e8d35aecd92150c6fa6843.png (其中404)的文件。我不知道我明白的地方应该是这个文件要得到保存。我假设它是拉动S3的文件,然后创建缩略图的地方的,但我不清楚的地方是,我所在的地方应该是从服务的。这是所有在本地devserver现在,使用memcached的(我在其中可以看到越来越缓存条目设置)。 PIL安装在我与JPEG / PNG支持环境。

That ends up generating a URL for the file like cache/84/8f/848fb078f2e8d35aecd92150c6fa6843.png (which 404s). I'm not sure I understand where this file is supposed to be getting stored. I am assuming it is pulling in the S3 file and then creating the thumbnail somewhere, but I am unclear where that is, and where I am supposed to be serving it from. This is all on the local devserver right now, using memcached (in which I can see is getting cache entries set). PIL is installed in my environment with jpeg/png support.

推荐答案

您可能想看看的 Django的存储器的,这是一个很好的 AWS S3 ,将与解决问题的you.Along还安装博托,作为Django的存储对博托依赖。

You might want to look at Django-storages it was a nice AWS S3 that would fix the issue for you.Along with that also install boto, as django-storage has a dependency on boto.

然后,你将需要添加以下到您的settings.py

Then you will have to add the following into your settings.py

import os

AWS_ACCESS_KEY_ID = os.environ.get('AWS_ACCESS_KEY_ID')
AWS_SECRET_ACCESS_KEY = os.environ.get('AWS_SECRET_ACCESS_KEY')
AWS_STORAGE_BUCKET_NAME = '<YOUR BUCKET NAME>'

STATICFILES_STORAGE = 'storages.backends.s3boto.S3BotoStorage'
DEFAULT_FILE_STORAGE = 'storages.backends.s3boto.S3BotoStorage'

STATIC_URL = 'http://' + AWS_STORAGE_BUCKET_NAME + '.s3.amazonaws.com/'
ADMIN_MEDIA_PREFIX = STATIC_URL + 'admin/'

那么你的缓存将被存储在AWS S3本身。

Then your cache will be stored on AWS S3 itself.

我希望它能帮助

注:出于安全原因,它是一个好主意,添加AWS_ACCESS_KEY_ID和AWS_SECRET_ACCESS_KEY的环境变量,而不仅仅是直接写下来的setting.py

Note: for security reasons its a good idea to add your AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY as environment variables instead of just writing them down in setting.py directly.