使用AWS S3为django提供英文上的静态文件? [英] Serve static files on heroku using AWS S3 for django?

查看:153
本文介绍了使用AWS S3为django提供英文上的静态文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在部署一个django应用程序使用英文和AWS S3静态文件,问题是我没有找到关于如何链接英雄的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 DB链接?这是可能的吗?

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

当然,我可以使用这样的静态网址,但问题是我想要显示不同的图像,而不是所有的图像。
在db 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 static file 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)并放在要求中.txt(pip freeze> requirements.txt)和settings.py add:

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/ 中的权限。

Finally you need to update the permissions in https://console.aws.amazon.com/

这篇关于使用AWS S3为django提供英文上的静态文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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