即成使用AWS S3 Django的Heroku的静态文件? [英] Serve static files on heroku using AWS S3 for django?
问题描述
我正在部署使用Heroku和AWS S3静态文件Django应用程序,问题是,我还没有找到如何与S3服务链接的Heroku的Postgres的数据库信息。
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分钟后消失。
This is how my model looks in the admin, but if i upload the images here, after 5 minutes disappears.
我当然可以使用静态URL这样的,但问题是,我想非常当然,不一样的图片显示不同的图像。
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.
所以,我的code需要是这样的,服务于不同的图像
So my code needs to be like this, to serve different images
我希望有人能帮助我,谢谢。
I hope some one could help me, Thanks.
推荐答案
我发现这个职位的解决方案:
I found the solution in this posts:
http://blog.doismellburning.co .UK / 2012/06/25 / Django的和静态档案/
http://offbytwo.com/2012/01/ 18 /部署-的Django到heroku.html
https://devcenter.heroku.com/articles/s3
基本上我需要下载博托(PIP安装博托),并把它放在requirements.txt(PIP冻结> 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:
最后,你需要更新 https://console.aws.amazon.com/
这篇关于即成使用AWS S3 Django的Heroku的静态文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!