Toli
Toli

Reputation: 5777

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

My goal is to create a Lamda function that does some permission checking (via external API) then signs a URL to allow uploads to S3.

I created the following AWS lambda function:

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
    }));
}

I get back a URL!

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

Then I created a JSBin to test it

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

But when I tried to upload a file I got

<?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>

EDIT Also it turns out that I can't upload a file as a multipart form. Instead I have to use XMLHttpRequest to send the file as a Binary to the signed url (https://devcenter.heroku.com/articles/s3-upload-node#direct-uploading)

Upvotes: 3

Views: 5244

Answers (1)

Matt Houser
Matt Houser

Reputation: 36073

Your bucket is in the US Standard region.

You need to generate a pre-signed upload URL using the US Standard endpoint.

Upvotes: 3

Related Questions