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)
Your bucket is in the US Standard region.
You need to generate a pre-signed upload URL using the US Standard endpoint.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With