Created
August 10, 2019 20:08
-
-
Save leemartin/18a0aae7da97a6c738ca0c5e8f2fc180 to your computer and use it in GitHub Desktop.
Get presigned upload url for S3 compatible with Uppy and Netlify Functions
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') | |
const s3 = new AWS.S3({ | |
accessKeyId: process.env.AWS_KEY, | |
secretAccessKey: process.env.AWS_SECRET, | |
useAccelerateEndpoint: true | |
}) | |
const getSignedUrlPromise = (operation, params) => { | |
return new Promise((resolve, reject) => { | |
return s3.getSignedUrl(operation, params, (err, url) => { | |
return err ? reject(err) : resolve(url) | |
}) | |
}) | |
} | |
exports.handler = async (event, context) => { | |
const key = event.queryStringParameters.key | |
try { | |
const url = await getSignedUrlPromise('putObject', { | |
Bucket: process.env.S3_BUCKET, | |
ContentType: 'image/jpeg', | |
Key: key | |
}).catch((err) => console.log(err)) | |
return { | |
statusCode: 200, | |
body: JSON.stringify({ | |
method: 'PUT', | |
url: url, | |
fields: [] | |
}) | |
} | |
} catch (err) { | |
console.log(err) | |
return { | |
statusCode: 500, | |
body: 'NOT OK' | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment