Skip to content

Instantly share code, notes, and snippets.

@paulogdm
Last active July 29, 2018 02:59
Show Gist options
  • Save paulogdm/b794243d36c8ec7d285a2f9347f8a9db to your computer and use it in GitHub Desktop.
Save paulogdm/b794243d36c8ec7d285a2f9347f8a9db to your computer and use it in GitHub Desktop.
[micro-s3-example] Get Signed URL
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