Skip to content

Instantly share code, notes, and snippets.

@lakshmantgld
Created February 29, 2020 12:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lakshmantgld/5be99c8fe7e322acd5f262f385efc324 to your computer and use it in GitHub Desktop.
Save lakshmantgld/5be99c8fe7e322acd5f262f385efc324 to your computer and use it in GitHub Desktop.
Lambda snippet to upload file to S3
'use strict';
const AWS = require('aws-sdk');
// Set the Region
AWS.config.update({
accessKeyId: '***********',
secretAccessKey: '*************',
region: 'us-east-1',
signatureVersion: 'v4'
});
// Create the S3 service object
const s3 = new AWS.S3();
module.exports.getPreSignedURLToPutToS3 = function(event, context, mainCallback) {
// What does this function do?
// 1. Receive the filename to be uploaded and Create a request param to make a request to create pre-signed URL to upload to S3
// 2. Make the request to S3
// 3. Get the pre-signed URL from S3
// 4. Send the pre-signed URL from S3 as response
// 1. Receive the filename to be uploaded and Create a request param to make a request to create pre-signed URL to upload to S3
let requestObject = JSON.parse(event["body"]);
const fileName = requestObject.fileName;
const fileType = requestObject.fileType;
const myBucket = 'jobobo-resumes';
s3.getSignedUrl('putObject', {
"Bucket": myBucket,
"Key": fileName,
"ContentType": fileType
}, function (err, url) {
if (err) {
mainCallback(null, err);
} else {
mainCallback(null, url);
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment