上传到签名的 AWS S3 Put URL returns a "Permanent Redirect "

Uploading to a Signed AWS S3 Put URL returns a "Permanent Redirect "

我的目标是创建一个 Lamda 函数来执行一些权限检查(通过外部 API)然后签署 URL 以允许上传到 S3。

我创建了以下 AWS lambda 函数:

var AWS = require('aws-sdk');
var uuid = require('uuid');
exports.handler = function(event, context) {
    // Some Auth Code is here to make sure user has permission
    var s3 = new AWS.S3();
    var url = s3.getSignedUrl('putObject', {
      Bucket: config.uploadBucket,
      Key: event.partnerName + '/images/' + uuid.v4()
    });
    context.succeed(JSON.stringify({
      url: url
    }));
}

我回来了URL!

https://s3-us-west-2.amazonaws.com/PATH/images/3f7ff785-1868-4a54-9ae2-e94228d4868d?AWSAccessKeyId=....&Expires=1455124250&Signature=...&x-amz-security-token=...

然后我创建了一个JSBin来测试它

<form method="POST" action="THE_URL_I_GOT">
  <input name="file" type="file"/>
  <input type="submit"/>
  </form>

但是当我尝试上传文件时,我得到了

<?xml version="1.0" encoding="UTF-8"?>
<Error>
    <Code>PermanentRedirect</Code>
    <Message>The bucket you are attempting to access must be addressed using the specified endpoint. Please send all future requests to this endpoint.</Message>
    <Bucket>MYBUCKETNAMEHERE</Bucket>
    <Endpoint>s3.amazonaws.com</Endpoint>
    <RequestId>...</RequestId>
    <HostId>.....</HostId>
</Error>

编辑 事实证明,我无法以多部分形式上传文件。相反,我必须使用 XMLHttpRequest 将文件作为二进制文件发送到已签名的 url (https://devcenter.heroku.com/articles/s3-upload-node#direct-uploading)

您的存储桶在美国标准区域。

您需要使用美国标准端点生成预签名上传 URL。