Skip to content

Instantly share code, notes, and snippets.

@curtis1000
Created December 15, 2022 18:12
Show Gist options
  • Save curtis1000/2040430bc067e90b1d64a92b92eba96d to your computer and use it in GitHub Desktop.
Save curtis1000/2040430bc067e90b1d64a92b92eba96d to your computer and use it in GitHub Desktop.
get signed url / lambda js
'use strict'
const AWS = require('aws-sdk')
AWS.config.update({ region: process.env.AWS_REGION || 'us-east-1' })
const s3 = new AWS.S3()
// Main Lambda entry point
exports.handler = async (event) => {
const result = await getUploadURL(event)
return result
}
const getUploadURL = async function(event) {
// this is probably where you want to auto generate some unique path
const path = 'subdir/myfile.webm'
const timestamp = Date.now()
const type = event.queryStringParameters.type || 'webm'
const s3Params = {
Bucket: 'shoutouts-media',
Key: `submissions/${path}`,
ContentType: `video/${type}`
//ACL: 'public-read' // Enable this setting to make the object publicly readable - only works if the bucket can support public objects
}
return new Promise((resolve, reject) => {
// Get signed URL
resolve({
"statusCode": 200,
"isBase64Encoded": false,
"headers": {
"Access-Control-Allow-Origin": "*"
},
"body": JSON.stringify({
"uploadURL": s3.getSignedUrl('putObject', s3Params)
})
})
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment