Last active
July 29, 2018 10:54
-
-
Save paulogdm/5f7579a530d3c1bb1281be0fd2f86746 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 generateUUID = (size = 10) => { | |
return randomBytes(Math.ceil(size * 3 / 4)) | |
.toString('base64') | |
.slice(0, size) | |
.replace(/\+/g, 'a') | |
.replace(/\//g, 'b') | |
} | |
const postS3 = async (req, res) => { | |
const {contentType} = await json(req) // we are requiring the ContenType from the request | |
const s3key = `${generateUUID()}.${contentType.split('/').pop()}` // this works for png, jpg, pdf, ... | |
const params = { | |
Bucket: config.bucket_name, | |
Fields: { key: s3key }, | |
Expires: 60 * 10, // 10 min | |
Conditions: [ | |
{ 'bucket': config.bucket_name }, | |
{ 'key': s3key // our generated key }, | |
{ 'acl': 'private' }, | |
{ 'Content-Type': contentType }, | |
['content-length-range', 8000, 8000000] // from 1KB to 1 MB | |
] | |
} | |
const result = await s3.createPresignedPost(params) | |
return Object.assign(result, | |
{ | |
'Content-Type': contentType, | |
'acl': 'private' | |
} | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment