Created
August 27, 2019 01:02
-
-
Save skabbes/1c8c3a34b7399b8d822730a31cc0e3eb to your computer and use it in GitHub Desktop.
This is a AWS S3 secure redirect to a file
This file contains hidden or 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
// secureRedirect redirects this fileId to be loaded directly from S3. | |
// if filename is provided, it will trigger a download of that filename | |
export function secureRedirect({ reply, fileId, filename }) { | |
const key = makeS3Key(fileId); | |
let contentDisposition; | |
if (filename && Buffer.from(filename, 'utf8').toString('ascii') === filename) { | |
contentDisposition = `attachment; filename="${filename}"`; | |
} else if (filename) { | |
const encodedFilename = Querystring.escape(filename); | |
contentDisposition = `attachment; filename*=UTF-8''${encodedFilename}`; | |
} | |
const url = S3.getSignedUrl('getObject', { | |
Bucket: BUCKET, | |
Key: key, | |
Expires: Math.floor(FilesConfig.expiresInMillis / 1000), | |
ResponseContentDisposition: contentDisposition, | |
}); | |
return reply().redirect(url); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment