Last active
April 27, 2024 11:29
-
-
Save AllardQuek/4f65af5755ea1b79ccbd305e06364376 to your computer and use it in GitHub Desktop.
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
const AWS = require('aws-sdk') | |
AWS.config.update({ region: process.env.AWS_REGION }) | |
const s3 = new AWS.S3() | |
const uploadBucket = 'test-bucket-357' // Replace this value with your bucket name! | |
const URL_EXPIRATION_SECONDS = 30000 // Specify how long the pre-signed URL will be valid for | |
// Main Lambda entry point | |
exports.handler = async (event) => { | |
return await getUploadURL(event) | |
} | |
const getUploadURL = async function(event) { | |
const randomID = parseInt(Math.random() * 10000000) | |
const Key = `${randomID}.mp3` // Random filename we will use when uploading files | |
// Get signed URL from S3 | |
const s3Params = { | |
Bucket: uploadBucket, | |
Key, | |
Expires: URL_EXPIRATION_SECONDS, | |
ContentType: 'image/jpeg' // Change this to the media type of the files you want to upload | |
} | |
return new Promise((resolve, reject) => { | |
// Get signed URL | |
let uploadURL = s3.getSignedUrl('putObject', s3Params) | |
resolve({ | |
"statusCode": 200, | |
"isBase64Encoded": false, | |
"headers": { | |
"Access-Control-Allow-Origin": "*" | |
}, | |
"body": JSON.stringify({ | |
"uploadURL": uploadURL, | |
"filename": Key | |
}) | |
}) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment