Skip to content

Instantly share code, notes, and snippets.

@erankitcs
Created March 14, 2021 11:41
Show Gist options
  • Save erankitcs/b335d48f5019a6a85e889582dab0f074 to your computer and use it in GitHub Desktop.
Save erankitcs/b335d48f5019a6a85e889582dab0f074 to your computer and use it in GitHub Desktop.
Lambda function to generate pre signed s3 write URL
'use strict';
const AWS = require('aws-sdk');
const crypto = require('crypto');
const s3 = new AWS.S3({signatureVersion: 'v4'});
const generateResponse = (status, message) => {
return {
statusCode: status,
headers: { 'Access-Control-Allow-Origin': '*' },
body : JSON.stringify(message)
}
};
const handler = (event, context, callback) => {
// Get the bucket name to upload to
const bucket = process.env.IMAGES_BUCKET_NAME;
if(!bucket) {
callback('No upload bucket set, please add an output bucket in the environment variables');
return;
}
if(!event.queryStringParameters.filename) {
callback('No file uploaded');
return;
}
// Get the filename from the query string parameters in the GET call
const filename = decodeURI(event.queryStringParameters.filename);
const directory = crypto.randomBytes(20).toString('hex');
const key = directory + '/' + filename;
const params = {
'Bucket': bucket,
'Fields': {
'key': key
},
'Conditions': [
{'acl': 'private'}
]
}
s3.createPresignedPost(params, function callbackToPost(error, data) {
if (error) {
// Failure
const response = generateResponse(400, error);
callback(null, response);
} else {
// Success
const response = generateResponse(200, data);
callback(null, response);
}
});
};
module.exports = {
handler
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment