【问题标题】:s3 failing on preflight even with cors all methods and origins即使使用所有方法和来源,s3 也会在预检中失败
【发布时间】:2016-05-17 22:04:10
【问题描述】:

我不明白...看起来很简单,但它在 put 请求的选项调用中失败了。 "预检响应无效"

S3 CORS:

<?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>
        <AllowedMethod>POST</AllowedMethod>
        <MaxAgeSeconds>3000</MaxAgeSeconds>
        <AllowedHeader>*</AllowedHeader>
    </CORSRule>
</CORSConfiguration>

网址:

http://localhost:9002/api/sign_s3?file_name=wCyQZgrKIS24oR4MV4hdPP-UXDNQ6P9MvOj6DRMPcJU.jpg&file_type=image/jpeg

到 s3 的 url(已删除键):

https://jayehtest.s3.amazonaws.com/wCyQZgrKIS24oR4MV4hdPP-UXDNQ6P9MvOj6DRMPcJU.jpg?AWSAccessKeyId=xxx&Content-Type=image%2Fjpeg&Expires=1454923884&Signature=xxx&x-amz-acl=public-read

生成put的代码:

    Aws.config.update({accessKeyId: process.env.AWS_ACCESS_KEY, secretAccessKey: process.env.AWS_SECRET_KEY});
    var s3 = new Aws.S3();
    var s3_params = {
        Bucket: process.env.S3_BUCKET,
        Key: req.query.file_name,
        //Expires: 120,
        //ContentType:  req.query.file_type,
        ContentType: "multipart/form-data",
        ACL: 'public-read-write',
    };
    s3.getSignedUrl('putObject', s3_params, function(err, data){
        console.log('got signed url!');
        if(err)
            res.status(500).json({error: err});
        else
            res.status(200).json({
                signed_request: data,
                url: 'https://'+process.env.S3_BUCKET+'.s3.amazonaws.com/'+req.query.file_name
            });
    });

发出put请求的代码:

    Superagent('get', '/api/sign_s3?file_name='+files[0].name+'&file_type='+files[0].type).end(function(err, response) {
            console.log('signs3 response: ', response);

            Superagent('put', response.body.signed_request)
            //Superagent('put', 'https://jayehtest.s3-us-west-2.amazonaws.com/10172871_10101161917002037_713851410092755221_n.jpg?AWSAccessKeyId=AKIAJOQ7AL7QGIGSXMQQ&Content-Type=image%2Fjpeg&Expires=1454947575&Signature=nBvjbkCLD5XsvFktEjnQOTdRFzM%3D&x-amz-acl=public-read'
            .set('x-amz-acl', 'public-read-write')
            .set('Content-Type', 'multipart/form-data')
            .attach('image', files[0])
            .end(cb);
        });

【问题讨论】:

  • 你为什么标记它node.js
  • 我正在使用 aws-sdk 来生成 put url。我将添加代码,但我也终于弄清楚如何修复它。花了 4 小时不间断的谷歌搜索和测试。

标签: node.js amazon-s3 cors


【解决方案1】:

S3 有很多愚蠢的层,但我需要完善一些东西:

  1. 需要完全匹配的标头。 Content-Type 需要字符与字符相同。

  2. 节点上的 x-amz-acl 和 ACL 都需要至少设置为 public-read

  3. 您需要在 s3 中生成一个存储桶策略,以授予您的 ACCESS_KEY 权限

  4. 它需要是一个 PUT 请求 [但这是我很快解决的问题]

注意:我创建了一个 IAM 用户,但后来没有使用它。 IAM 用户甚至没有工作。它适用于公钥。

【讨论】:

    猜你喜欢
    • 2016-08-14
    • 2014-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-09
    • 1970-01-01
    • 2021-09-06
    • 1970-01-01
    相关资源
    最近更新 更多