Created
January 30, 2020 12:57
-
-
Save secretshardul/5158564307bc420355adab18a4bb46f7 to your computer and use it in GitHub Desktop.
Generate pre-signed URL for S3 upload on AWS Lambda.
This file contains hidden or 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
const AWS = require('aws-sdk'); | |
AWS.config.update({ | |
accessKeyId: 'YOUR_KEY_ID', // Generated on step 1 | |
secretAccessKey: 'SECRET', // Generated on step 1 | |
region: 'ap-south-1', // Must be the same as your bucket | |
signatureVersion: 'v4', | |
}); | |
const params = { | |
Bucket: 'YOUR_BUCKET_NAME', | |
Key: 'FILE_NAME' | |
}; | |
const options = { | |
signatureVersion: 'v4', | |
region: 'ap-south-1', | |
endpoint: new AWS.Endpoint('https://s3.ap-south-1.amazonaws.com/com.shardul.s3uploadbucket') | |
}; | |
const client = new AWS.S3(options); | |
exports.handler = async (event) => { | |
const signedURL = await (new Promise((resolve, reject) => { | |
client.getSignedUrl('putObject', params, (err, data) => { | |
if (err) { | |
reject(err); | |
} | |
else { | |
resolve(data); | |
} | |
}); | |
})); | |
return signedURL; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment