我正在使用 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/