上传到签名的AWS S3 Put URL会返回“永久重定向”

时间:2016-02-10 17:26:33

标签: amazon-web-services amazon-s3 aws-lambda

我的目标是创建一个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
    }));
}

我找回了一个网址!

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

1 个答案:

答案 0 :(得分:1)

您的水桶位于美国标准区域。

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