Reputation: 5777
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!
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
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