Django S3 Di上的CORS错误

2024-10-02 22:37:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用djangos3direct将文件上传到S3存储桶中

我使用以下配置

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

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

我设置bucket,CORS配置如下

^{pr2}$

但是,当我尝试在Django admin中上载文件时,会出现以下错误:

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.

Tags: 文件keynameawsidsecrets3bucket
2条回答

同样的问题,除了我一直从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相反的方法来解决这个问题。我把S3DIRECT_REGION放在settings中,删除它(直接查询s3.amazonaws.com)后,问题就解决了!在

我在django-s3direct中遇到了同样的问题,甚至在添加了CORS值之后。对我来说,在添加了 S3DIRECT_地区='ap-东南-1' 您可以从here中选择您的区域

相关问题 更多 >