Last active
July 29, 2018 02:59
-
-
Save paulogdm/b794243d36c8ec7d285a2f9347f8a9db to your computer and use it in GitHub Desktop.
[micro-s3-example] Get Signed URL
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 getS3 = async (req, res) => { | |
const s3key = `${req.url.substr(1)}` // we need to remove the first backslash from the url | |
const params = { | |
Bucket: config.bucket_name, | |
Key: s3key, | |
Expires: 60 * 60 * 24 // 1 day | |
} | |
try { | |
// key exists? | |
await s3.headObject({ | |
Bucket: params.Bucket, | |
Key: params.Key | |
}).promise() | |
// if it does, generate a signed url | |
const url = await s3.getSignedUrl('getObject', params) | |
return url | |
} catch (err) { | |
if (err.statusCode === 404) { | |
throw createError(404, 'Not Found') | |
} else throw err | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment