CORS问题与PreSigned S3 URL进行文件上传

我尝试在eu-west-2地区的S3存储桶中上传文件:

在我编码的django-view中:

conn = boto.connect_s3(settings.AWS_ACCESS_KEY_ID,settings.AWS_SECRET_ACCESS_KEY,host=settings.AWS_S3_HOST)
     try:
        conn = boto.connect_s3(settings.AWS_ACCESS_KEY_ID,settings.AWS_SECRET_ACCESS_KEY,host=settings.AWS_S3_HOST)
    except:
        print "S3connection Failed"
    else:
        print "S3Connection OK"

然后我生成预先签名的网址

try:
    S3url = conn.generate_url_sigv4(secondsPerDay, "PUT", bucket=settings.AWS_STORAGE_BUCKET_NAME, key=path, force_http=True)
except:
    print "generate_url_sigv4 failed"
else:
    print "generate_url_sigv4 OK"
    print S3url

和S3url是:S3url = https://MyBucketName.s3-eu-west-2.amazonaws.com/pathtofile/0.MOV?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Expires=86400&X-Amz- SignedHeaders =宿主X-AMZ-签名= AWSsignature&X-AMZ-日期= 20170724T120524Z&X-AMZ-凭证=的myKey%2F20170724%2Feu西-2%2Fs3%2Faws4_request

使用卷曲我可以上传一个视频文件:

curl -v -T "0.MOV" --header "Content-Type:binary/octet-stream" "https://MyBucketName.s3-eu-west-2.amazonaws.com/pathtofile/0.mp4?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Expires=86400&X-Amz-SignedHeaders=host&X-Amz-Signature=AWSsignature&X-Amz-Date=20170724T122024Z&X-Amz-Credential=MyKey%2Feu-west-2%2Fs3%2Faws4_request"

所以对于我来说,在Django / python中生成预先签名的url是可以的。

感谢如何正确上传对象到AWS S3? 贡献者

问题出现在我尝试使用预先通过的URL和ajax请求时。 Google Chrome浏览器指出:

Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://127.0.0.1:8000' is therefore not allowed access. The response had HTTP status code 403.

我尝试了许多组合

settings = $.extend(true, {}, {
    xhr: function () {
        var xhrobj = $.ajaxSettings.xhr();
        return self._initXhr(xhrobj, previewId, self.getFileStack().length);
    },
    url:S3url,
    type: 'PUT',
    cache: false,
    processData: false,
    success: fnSuccess,
    complete: fnComplete,
    error: fnError,
    crossDomain: true,
    headers: {
            'Access-Control-Allow-Headers': '*',
            'Content-Type': 'binary/octet-stream',
        },
}, self.ajaxSettings);

self.ajaxRequests.push($.ajax(settings));

显然,浏览器和curl阻止了对另一台服务器的请求。 该网页来自http://127.0.0.1:8000,并且请求是针对s3-eu-west-2.amazonaws.com。

卷曲不采取这种预防措施。

顺便说一句,我像这样配置S3存储桶:

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

这是除了我添加/行之外的默认配置。

那么我怎么能解决这个CORS问题,我错过了什么?

谢谢

链接地址: http://www.djcxy.com/p/39045.html

上一篇: CORS issue with a PreSigned S3 URL for file upload

下一篇: AWS S3 Presigned Request Cache