简体   繁体   English

Django 和 S3 存储桶 aws 管理员 Static 文件

[英]Django and S3 Bucket aws Admin Static files

I have a django project, i wanted to configure S3 Bucket in order to store the static files.我有一个 django 项目,我想配置 S3 Bucket 以存储 static 文件。 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:创建后,站点会加载,但不会从存储桶中检索文件,因此即使管理页面也没有 CSS,如屏幕截图所示: 从 settings.py 激活存储桶后

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:存储桶策略 (CORS) 设置为:

[
    {
        "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, ...)我也尝试过(py manage.py collectstatic)而不手动上传,管理文件夹出现在存储桶中,但即使在使用“Collectstatic”之后我仍然遇到同样的问题(没有 css,没有图像,...)

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:尝试将完整的 aws 链接添加到您的媒体和 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.此外,检查浏览器中生成的 aws 中的 static 文件链接以查看响应内容可能会进一步帮助您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM