python - Django S3 Direct 上的 CORS 错误

标签 python django amazon-web-services amazon-s3

我正在使用 Django S3 Direct 将文件上传到 S3 存储桶

我使用以下配置

AWS_SECRET_ACCESS_KEY = ''
AWS_ACCESS_KEY_ID = ''
AWS_STORAGE_BUCKET_NAME = ''

这些值是在 IAM 管理控制台中生成的

我设置了bucket,CORS配置如下

<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
    <CORSRule>
        <AllowedOrigin>*</AllowedOrigin>
        <AllowedMethod>PUT</AllowedMethod>
        <AllowedMethod>POST</AllowedMethod>
        <AllowedMethod>GET</AllowedMethod>
        <MaxAgeSeconds>3000</MaxAgeSeconds>
        <AllowedHeader>*</AllowedHeader>
    </CORSRule>
</CORSConfiguration>

但是,当我尝试在 Django 管理中上传文件时,出现以下错误:

XMLHttpRequest cannot load https://s3.amazonaws.com/<bucketname>. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8000' is therefore not allowed access.

最佳答案

同样的问题,只是我一直从 AWS 收到 301。经过一番挖掘,我发现了这条评论 comment :

From what I know, S3 is cross-region, which means that you do not need to specify a region to upload files to, as it will uploads to one and replicates to other regions behind the scenes. Therefor endpoint = 's3.amazonaws.com' If your bucket called 'test', you will upload to 'test.s3.amazonaws.com', not to 'test.s3-eu-west-1.amazonaws.com'.

所以我解决这个问题的方法与@Kanhaiya 建议的相反。我在 settings 中有 S3DIRECT_REGION,删除它后(直接查询 s3.amazonaws.com),问题就解决了!

关于python - Django S3 Direct 上的 CORS 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33273074/

相关文章:

python - SGDClassifier 在 MNIST 上的使用

python - Google App Engine - 使用任务队列或延迟作业

node.js - npm-start 未在 aws cloudformation 用户数据中运行

python - 亚马逊广告 API ItemSearch : Get more than 10 pages/100 results?

amazon-web-services - 将文件复制到 S3 中的文件夹中,最终位于父文件夹中

python - Pytorch,通过第一个和最后一个元素扩展张量的有效方法

python - Numpy.cumsum 反向

python - 如何检查 Django 应用程序的性能?

python - Django表单仅在表单有效时保存相关对象

python - 如何调用应用程序文件夹外的模板?