Lambda snippet to upload file to S3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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