简体   繁体   English

设置Django从Amazon S3提供媒体文件

[英]Setting Django to serve media files from Amazon S3

I'm trying to deploy my project in Heroku but the media files (images) are deleted after a while, so someone told me that i need to use a service called "Amazon S3", my question is, how to configure my prject to use that service. 我正在尝试在Heroku中部署我的项目,但过一会后媒体文件(图像)被删除,所以有人告诉我我需要使用一个名为“ Amazon S3”的服务,我的问题是,如何配置我的项目以使用该服务。 Can somebody help me? 有人可以帮我吗?

Check out this for starters http://docs.aws.amazon.com/AmazonS3/latest/dev/WebsiteHosting.html 查看此入门指南http://docs.aws.amazon.com/AmazonS3/latest/dev/WebsiteHosting.html

Basically you don't need Django to serve anything here, instead you need to make sure the static references point to another domain (the S3 one) instead of your Heroku one. 基本上,您不需要Django在这里提供任何服务,而是需要确保静态引用指向另一个域(S3域)而不是Heroku域。

You could follow the steps in this article: 您可以按照本文中的步骤进行操作:

http://blog.doismellburning.co.uk/2012/07/14/using-amazon-s3-to-host-your-django-static-files/ http://blog.doismellburning.co.uk/2012/07/14/using-amazon-s3-to-host-your-django-static-files/

But a little tutorial to do that could be: 但是,可以这样做的一些教程可能是:

Step 1 - Install boto and django-storages: 第1步 -安装boto和django-storages:

$ pip install boto django-storages

Add django-storages to INSTALLED_APPS: 将django-storages添加到INSTALLED_APPS:

INSTALLED_APPS += ('storages',)

Step 2 - Create your S3 bucket: 第2步 -创建S3存储桶:

Go to https://console.aws.amazon.com/s3/home and create it. 转到https://console.aws.amazon.com/s3/home并创建它。

Step 3 - Get your credentials: 第3步 -获取凭据:

Go to https://console.aws.amazon.com/iam/home?#security_credential , click on "Access Keys" and create it. 转到https://console.aws.amazon.com/iam/home?#security_credential ,单击“访问密钥”并创建它。

Step 4 - Add your credentials to django settings: 第4步 -将凭据添加到Django设置:

First of all, create a file called s3utils.py in the project folder, with the follow content: 首先,在项目文件夹中创建一个名为s3utils.py的文件,其内容如下:

from storages.backends.s3boto import S3BotoStorage                                            
StaticRootS3BotoStorage = lambda: S3BotoStorage(location='static')              
MediaRootS3BotoStorage  = lambda: S3BotoStorage(location='media') 

I prefer to use all of this configurations as environment variables, so I suggest you to do: 我更喜欢将所有这些配置用作环境变量,因此建议您这样做:

$ heroku config:set AWS_ACCESS_KEY_ID=YOUR_ACCESS_KEY AWS_SECRET_ACCESS_KEY=YOUR_SECRET_ACCESS_KEY S3_BUCKET_NAME=YOUR_BUCKET_NAME

And after that, put this in your settings: 然后,将其放在您的设置中:

AWS_STORAGE_BUCKET_NAME = os.environ['S3_BUCKET_NAME']                          
MEDIA_ROOT = '/media/'                                                          
S3_URL = 'http://%s.s3.amazonaws.com/' % AWS_STORAGE_BUCKET_NAME                
MEDIA_URL = S3_URL + MEDIA_ROOT                                                 
DEFAULT_FILE_STORAGE = 'YOUR_PROJECT.s3utils.MediaRootS3BotoStorage'                 
STATICFILES_STORAGE = 'YOUR_PROJECT.s3utils.StaticRootS3BotoStorage'                 
AWS_ACCESS_KEY_ID = os.environ['AWS_ACCESS_KEY_ID']                             
AWS_SECRET_ACCESS_KEY = os.environ['AWS_SECRET_ACCESS_KEY']  

Step 5 - Run collectstatic again 第5步 -再次运行collectstatic

You'll need collect static files again, so that it will be put in Amazon. 您将需要再次收集静态文件,以便将其放置在Amazon中。

heroku run python manage.py collectstatic

I hope it helps! 希望对您有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM