将Cloudfront与Django S3Boto结合使用 [英] Using Cloudfront with Django S3Boto

查看:107
本文介绍了将Cloudfront与Django S3Boto结合使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经成功设置我的应用程序以使用S3来存储所有静态和媒体文件。但是,我想上传到S3(当前操作),但是可以从我设置的Cloudfront实例中进行服务。我曾尝试将设置调整为cloudfront url,但是它不起作用。我如何才能上传到S3并从Cloudfront提供服务?



设置

  AWS_S3_CUSTOM_DOMAIN ='%s.s3.amazonaws.com'%AWS_STORAGE_BUCKET_NAME 

DEFAULT_FILE_STORAGE ='app.custom_storages.MediaStorage'
STATICFILES_STORAGEs ='app.custom .StaticStorage'

STATICFILES_LOCATION ='静态'
MEDIAFILES_LOCATION ='media'

STATIC_URL = https://s3-eu-west-1.amazonaws。 com / app /%s /%(STATICFILES_LOCATION)
MEDIA_URL = https://%s /%s /%(AWS_S3_CUSTOM_DOMAIN,MEDIAFILES_LOCATION)

custom_storages.py

  django.conf从storages.backends.s3bo导入设置
导入S3BotoStorage

class StaticStorage(S3BotoStorage):
location = settings.STATICFILES_LOCATION

class MediaStorage (S3BotoStorage):
位置= settings.MEDIAFILES_LOCATION


解决方案

您的代码几乎完整,除了您没有将自己的Cloudfront域添加到STATIC_URL / MEDIA_URL和自定义存储中。



在详细信息,您必须先安装依赖项

  pip install django-storages-redux boto 

将所需的设置添加到Django设置文件中

  INSTALLED_APPS =(
...
'storages',
...


AWS_STORAGE_BUCKET_NAME ='mybucketname'
AWS_CLOUDFRONT_DOMAIN ='xxxxxxxx.cloudfront.net'
AWS_ACCESS_KEY_ID = get_secret( AWS_ACCESS_KEY_ID)
AWS_SECRET_ACCESS_KEY = get_secret( AWS_SECRET_ACCESS_KEY)

MEDIAFILES_LOROCATION ='media' ='/%s /'%MEDIAFILES_LOCATION
MEDIA_URL ='//%s /%s /'%(AWS_CLOUDFRONT_DOMAIN,MEDIAFILES_LOCATION)
DEFAULT_FILE_STORAGE ='app.custom_storages.MediaStorage'

STATICFILES_LOCATION ='静态'
STATIC_ROOT ='/%s /'%S TATICFILES_LOCATION
STATIC_URL ='//%s /%s /'%(AWS_CLOUDFRONT_DOMAIN,STATICFILES_LOCATION)
STATICFILES_STORAGE ='app.custom_storages.StaticStorage'

您的自定义存储需要进行一些修改以显示资源的云前域,而不是S3域:



<$从django.conf导入p $ p> 从storages.backends.s3bo导入设置
导入S3BotoStorage

class StaticStorage(S3BotoStorage):
上传到'mybucket / static /',从'cloudfront.net/static/'\"\"\"
location = settings.STATICFILES_LOCATION

def __init __(self,* args,** kwargs)提供服务:
kwargs ['custom_domain'] = settings.AWS_CLOUDFRONT_DOMAIN
super(StaticStorage,self).__ init __(* args,** kwargs)

class MediaStorage(S3BotoStorage):
上载到'mybucket / media /',从'cloudfront.net/media/'\"\"
location = settings.MEDIAFILES_LOCATION

def __init __(self ,* args,** kwargs):
kwargs ['custom_domain'] =设置。AWS_CLOUDFRONT_DOMAIN
super(MediaStorage,self).__ init __(* args,** kwargs)

这就是您所需要的,假设您的存储桶和Cloudfront域已正确链接,并且用户的AWS_ACCESS_KEY具有对存储桶的访问权限。此外,根据您的用例,您可能希望将s3存储桶项设置为所有人都只读访问。


I have successfully set up my app to use S3 for storing all static and media files. However, I would like to upload to S3 (current operation), but serve from a cloudfront instance I have set up. I have tried adjusting settings to the cloudfront url but it does not work. How can I upload to S3 and serve from Cloudfront please?

Settings

AWS_S3_CUSTOM_DOMAIN = '%s.s3.amazonaws.com' % AWS_STORAGE_BUCKET_NAME

DEFAULT_FILE_STORAGE = 'app.custom_storages.MediaStorage'
STATICFILES_STORAGE = 'app.custom_storages.StaticStorage'

STATICFILES_LOCATION = 'static'
MEDIAFILES_LOCATION = 'media'

STATIC_URL = "https://s3-eu-west-1.amazonaws.com/app/%s/" % (STATICFILES_LOCATION)
MEDIA_URL = "https://%s/%s/" % (AWS_S3_CUSTOM_DOMAIN, MEDIAFILES_LOCATION)

custom_storages.py

from django.conf import settings
from storages.backends.s3boto import S3BotoStorage

class StaticStorage(S3BotoStorage):
    location = settings.STATICFILES_LOCATION

class MediaStorage(S3BotoStorage):
    location = settings.MEDIAFILES_LOCATION

解决方案

Your code is almost complete except you are not adding your cloudfront domain to STATIC_URL/MEDIA_URL and your custom storages.

In detail, you must first install the dependencies

pip install django-storages-redux boto

Add the required settings to your django settings file

INSTALLED_APPS = (
    ...
    'storages',
    ...
)

AWS_STORAGE_BUCKET_NAME = 'mybucketname'
AWS_CLOUDFRONT_DOMAIN = 'xxxxxxxx.cloudfront.net'
AWS_ACCESS_KEY_ID = get_secret("AWS_ACCESS_KEY_ID")
AWS_SECRET_ACCESS_KEY = get_secret("AWS_SECRET_ACCESS_KEY")

MEDIAFILES_LOCATION = 'media'
MEDIA_ROOT = '/%s/' % MEDIAFILES_LOCATION
MEDIA_URL = '//%s/%s/' % (AWS_CLOUDFRONT_DOMAIN, MEDIAFILES_LOCATION)
DEFAULT_FILE_STORAGE = 'app.custom_storages.MediaStorage'

STATICFILES_LOCATION = 'static'
STATIC_ROOT = '/%s/' % STATICFILES_LOCATION
STATIC_URL = '//%s/%s/' % (AWS_CLOUDFRONT_DOMAIN, STATICFILES_LOCATION)
STATICFILES_STORAGE = 'app.custom_storages.StaticStorage'

Your custom storages need some modification to present the cloudfront domain for the resources, instead of the S3 domain:

from django.conf import settings
from storages.backends.s3boto import S3BotoStorage

class StaticStorage(S3BotoStorage):
"""uploads to 'mybucket/static/', serves from 'cloudfront.net/static/'"""
    location = settings.STATICFILES_LOCATION

    def __init__(self, *args, **kwargs):
        kwargs['custom_domain'] = settings.AWS_CLOUDFRONT_DOMAIN
        super(StaticStorage, self).__init__(*args, **kwargs)

class MediaStorage(S3BotoStorage):
"""uploads to 'mybucket/media/', serves from 'cloudfront.net/media/'"""
    location = settings.MEDIAFILES_LOCATION

    def __init__(self, *args, **kwargs):
        kwargs['custom_domain'] = settings.AWS_CLOUDFRONT_DOMAIN
        super(MediaStorage, self).__init__(*args, **kwargs)

And that is all you need, assuming your bucket and cloudfront domain are correctly linked and the user's AWS_ACCESS_KEY has access permissions to your bucket. Additionally, based on your use case, you may wish to make your s3 bucket items read-only accessible by everyone.

这篇关于将Cloudfront与Django S3Boto结合使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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