Skip to content

Instantly share code, notes, and snippets.

@skabbes
Created August 27, 2019 01:02
Show Gist options
  • Save skabbes/1c8c3a34b7399b8d822730a31cc0e3eb to your computer and use it in GitHub Desktop.
Save skabbes/1c8c3a34b7399b8d822730a31cc0e3eb to your computer and use it in GitHub Desktop.
This is a AWS S3 secure redirect to a file
// 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