使用 AWS S3 for django 在 heroku 上提供静态文件? [英] Serve static files on heroku using AWS S3 for django?
问题描述
我正在使用 heroku 和 AWS S3 为静态文件部署 django 应用程序,问题是我没有找到有关如何将 heroku 的 Postgres DB 与 S3 服务链接的信息.
i'm deploying a django application using heroku and AWS S3 for static files, the problem is that i haven't found information on how to link the Postgres DB of heroku with the S3 service.
我已经设置了 AWS_ACCESS_KEY_ID、AWS_SECRET_ACCESS_KEY、S3_BUCKET、添加 CORS 配置,但我的问题是如何将 S3 存储与 Heroku 的 postgres 数据库链接?这可能吗?
I've already set the AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, S3_BUCKET, Add CORS Configuration, but my question is how can i link the S3 storage with the postgres DB of Heroku? Is this possible?
这是我的模型在管理员中的外观,但如果我在这里上传图像,5 分钟后就会消失.模型上传图片 http://ishopss.com/imageUpload.png
This is how my model looks in the admin, but if i upload the images here, after 5 minutes disappears. Image Upload for the model http://ishopss.com/imageUpload.png
当然我可以使用这样的静态 url,但问题是我想显示不同的图像,而不是所有图像都相同.服务于数据库 http://ishopss.com/for.png
Of course i can use static urls like this, but the problem is that i want to display different images for very course, not the same image for all. serve in the db http://ishopss.com/for.png
所以我的代码需要像这样,以提供不同的图像aws 静态文件 http://ishopss.com/course.png
So my code needs to be like this, to serve different images aws static file http://ishopss.com/course.png
希望有人能帮助我,谢谢.
I hope some one could help me, Thanks.
推荐答案
我在这篇文章中找到了解决方案:
I found the solution in this posts:
http://blog.doismellburning.co.uk/2012/06/25/django-and-static-files/
http://offbytwo.com/2012/01/18/deploying-django-to-heroku.html
https://devcenter.heroku.com/articles/s3
基本上我需要下载boto(pip install boto)并将其放入requirements.txt(pip freeze > requirements.txt),并在settings.py中添加:
Basically i need to download boto (pip install boto) and put it in requirements.txt(pip freeze > requirements.txt), and in settings.py add:
INSTALLED_APPS = ('storages',)
AWS_ACCESS_KEY_ID = 'xxxxxx'
AWS_SECRET_ACCESS_KEY = 'xxxxxxx'
AWS_STORAGE_BUCKET_NAME = 'bucket_name'
STATICFILES_STORAGE = 'storages.backends.s3boto.S3BotoStorage'
DEFAULT_FILE_STORAGE = 'storages.backends.s3boto.S3BotoStorage'
STATIC_URL = 'http://s3.amazonaws.com/%s' % AWS_STORAGE_BUCKET_NAME + '/'
然后你需要设置'AWS_ACCESS_KEY_ID'、'AWS_SECRET_ACCESS_KEY'、'AWS_STORAGE_BUCKET_NAME'
Then you need to set 'AWS_ACCESS_KEY_ID', 'AWS_SECRET_ACCESS_KEY', 'AWS_STORAGE_BUCKET_NAME'
使用
$ heroku config:set AWS_ACCESS_KEY_ID=xxx AWS_SECRET_ACCESS_KEY=yyy
$ heroku config:set S3_BUCKET_NAME=appname-assets
更新 html 中的 src 和 href:
Update the src and href in your html:
url http://ishopss.com/url.png
最后您需要更新https://console.aws.amazon.com/
这篇关于使用 AWS S3 for django 在 heroku 上提供静态文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!