Django 和 S3 Bucket aws Admin 静态文件 [英] Django and S3 Bucket aws Admin Static files

查看:35
本文介绍了Django 和 S3 Bucket aws Admin 静态文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 django 项目,我想配置 S3 Bucket 以存储静态文件.创建后,站点会加载,但不会从存储桶中检索文件,因此即使是管理页面也没有 CSS,如屏幕截图所示:

这是用于存储桶配置的设置:

STATIC_URL = '/static/'MEDIA_URL = '/图片/'STATICFILES_DIRS = [os.path.join(BASE_DIR,'静态')]MEDIA_ROOT = os.path.join(BASE_DIR, '静态/图像')AWS_ACCESS_KEY_ID = '************'AWS_SECRET_ACCESS_KEY = '********************'AWS_STORAGE_BUCKET_NAME = 'BUCKET_NAME'AWS_S3_FILE_OVERWRITE = FalseAWS_DEFAULT_ACL = 无DEFAULT_FILE_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'STATICFILES_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'

当然,我将(存储)添加到已安装的应用程序中.存储桶策略 (CORS) 设置为:

<预><代码>[{允许的标题":[*"],允许的方法":[获取",放置",邮政",删除"],允许的起源":[*"],ExposeHeaders":[x-amz-服务器端加密",x-amz-request-id",x-amz-id-2"],MaxAgeSeconds":3000

我也试过(py manage.py collectstatic)如果不手动上传,存储桶中会出现一个 admin 文件夹,但即使在使用Collectstatic"之后仍然存在我仍然有同样的问题(没有 css,没有图像,...)

我一直在兜圈子,找不到解决方案,如果有人能提供帮助,我将不胜感激.

提前致谢

解决方案

尝试将完整的 aws 链接添加到您的媒体和静态 url:

STATIC_URL = "https://%s/%s/";% (AWS_S3_CUSTOM_DOMAIN, AWS_LOCATION)MEDIA_URL = "https://%s/%s/";% (AWS_S3_CUSTOM_DOMAIN, AWS_LOCATION_MEDIA)

确保添加这些字段希望它有效.

AWS_S3_SIGNATURE_VERSION = 's3v4'AWS_S3_REGION_NAME = 'us-east-2'AWS_S3_ADDRESSING_STYLE = 虚拟"

此外,检查来自浏览器中生成的 aws 的静态文件链接以查看响应可能会进一步帮助您.

I have a django project, i wanted to configure S3 Bucket in order to store the static files. Once create, the site loads but there files are not retrieved from the bucket so there is no CSS even for the admin page as in the screen shot :

Here is the settings used for the bucket configurations :

STATIC_URL = '/static/'

MEDIA_URL = '/images/'

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static')
]

MEDIA_ROOT = os.path.join(BASE_DIR, 'static/images')




AWS_ACCESS_KEY_ID = '************'
AWS_SECRET_ACCESS_KEY = '********************'
AWS_STORAGE_BUCKET_NAME = 'BUCKET_NAME'
AWS_S3_FILE_OVERWRITE = False
AWS_DEFAULT_ACL = None
DEFAULT_FILE_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'
STATICFILES_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'

offcourse i added (storages) to the installed apps. The bucket policy (CORS) is set to :

[
    {
        "AllowedHeaders": [
            "*"
        ],
        "AllowedMethods": [
            "GET",
            "PUT",
            "POST",
            "DELETE"
        ],
        "AllowedOrigins": [
            "*"
        ],
        "ExposeHeaders": [
            "x-amz-server-side-encryption",
            "x-amz-request-id",
            "x-amz-id-2"
        ],
        "MaxAgeSeconds": 3000

I tried also (py manage.py collectstatic) without uploading manually, an admin folder appears in the bucket, but still even after using "Collectstatic" i still have the same problem (no css, no images, ...)

I'm running in circles and can't find a solution, i'd very much appreciate if someone could help.

Thank you in advance

解决方案

Try to add the full aws link to your media and static url:

STATIC_URL = "https://%s/%s/" % (AWS_S3_CUSTOM_DOMAIN, AWS_LOCATION)
MEDIA_URL = "https://%s/%s/" % (AWS_S3_CUSTOM_DOMAIN, AWS_LOCATION_MEDIA)

make sure to add these fields hope it works.

AWS_S3_SIGNATURE_VERSION = 's3v4'
AWS_S3_REGION_NAME = 'us-east-2'
AWS_S3_ADDRESSING_STYLE = "virtual"

Also, checking the static file link from aws generated in the browser to see what the response is might help you further.

这篇关于Django 和 S3 Bucket aws Admin 静态文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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