简体   繁体   English

Django 在 AWS S3 中托管 static 个文件,在尝试访问管理字体文件时导致 CORS 错误

[英]Django hosting static files in AWS S3 causing CORS error when trying to access admin font files

I setup Django to store all static files on S3 and with the "collectstatic" command, all the admin files where copied and when I visit the admin site the CSS and JS files are sourced from the S3 bucket correctly.我设置 Django 以将所有 static 文件存储在 S3 上,并使用“collectstatic”命令复制所有管理文件,当我访问管理站点时,CSS 和 JS 文件正确地来自 S3 存储桶。 For example:例如:

https://bucket.s3.amazonaws.com/static/admin/css/base.css
https://bucket.s3.amazonaws.com/static/admin/js/vendor/jquery/jquery.js

But when it tries to access the Font files.woff I get the following error:但是当它尝试访问 Font files.woff 时,出现以下错误:

Access to font at 'https://bucket.s3.amazonaws.com/static/admin/fonts/Roboto-Light-webfont.woff' from origin 'http://localhost:8000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

It's weird that it gives me access to all the files except the font files:奇怪的是,它让我可以访问除字体文件之外的所有文件:

在此处输入图像描述

Configuring the following in S3 Bucket Permissions > CORS fixed the issue.在 S3 存储桶权限 > CORS 中配置以下内容修复了该问题。

[
    {
        "AllowedHeaders": [
            "Authorization"
        ],
        "AllowedMethods": [
            "GET"
        ],
        "AllowedOrigins": [
            "*"
        ],
        "ExposeHeaders": [],
        "MaxAgeSeconds": 3000
    },
    {
        "AllowedHeaders": [
            "*"
        ],
        "AllowedMethods": [
            "HEAD",
            "GET",
            "PUT",
            "POST",
            "DELETE"
        ],
        "AllowedOrigins": [
            "*"
        ],
        "ExposeHeaders": [
            "ETag",
            "x-amz-meta-custom-header"
        ]
    }
]

I remember i faced this issue before with Google Cloud Storage.我记得我之前在使用 Google Cloud Storage 时遇到过这个问题。

You have to make your bucket allow origin cross to your domain.您必须让您的存储桶允许来源交叉到您的域。

暂无
暂无

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

相关问题 Django Admin S3 私有媒体文件 - Django Admin S3 Private Media Files Django + AWS s3 可以上传文件但不能访问它们 - Django + AWS s3 can upload files but not access them 我收到多个错误:尝试在 aws s3 存储桶中上传文件时出现意外字段 - I am getting multer error : unexpected field when i was trying to upload files in aws s3 bucket Django AWS S3 媒体文件 - Django AWS S3 media files AWS athena 在尝试查询 S3 中已在 Glue 数据目录中编目的文件时出错 - AWS athena giving error when trying to query files in S3 that have already been catalogued in Glue data catalog 尝试从 AWS Lambda 访问 S3 时访问被拒绝 - Access Denied when trying to access S3 from AWS Lambda 我如何使用 Aws amplify 和 s3 存储来私下存储项目,但允许管理员访问所有文件 - How can i use Aws amplify and s3 storage to store items privately, but allow admin to access all files 从 AWS S3 存储桶在 Express 中提供 static 个文件 - Serve static files in Express from AWS S3 bucket 为什么在访问 AWS S3 存储桶时出现“CORS header 'Access-Control-Allow-Origin' missing”错误? - Why am I having the "CORS header ‘Access-Control-Allow-Origin’ missing" error when accessing an AWS S3 bucket? AWS S3 - 尝试从 aws web 控制台访问时出现 400 错误; 在 AWS 主页的 devtools 中也出现很多错误 - AWS S3 - Getting 400 error when trying to access from aws web console; Also getting lots of errors in devtools at AWS home
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM