Skip to content

Instantly share code, notes, and snippets.

@skabbes
Created August 27, 2019 01:18
Show Gist options
  • Save skabbes/3f7dba1e1c9ea422c6d14df3d2163932 to your computer and use it in GitHub Desktop.
Save skabbes/3f7dba1e1c9ea422c6d14df3d2163932 to your computer and use it in GitHub Desktop.
/* Fetch a file from s3 as a Stream
*
* @param fileId the fileId to fetch
* @return {stream: Stream, meta, contentType, contentLength}
*/
export async function fetchStream({ fileId }) {
try {
const key = makeS3Key(fileId);
const fileOpts = { Bucket: BUCKET, Key: key };
const { stream, data } = await Promise.props({
stream: S3.getObject(fileOpts).createReadStream(),
data: fetchMeta({ fileId }),
});
const contentLength = data.ContentLength;
return { stream, meta: data.meta, contentType: data.contentType, contentLength };
} catch (err) {
if (err.name === 'NoSuchKey') {
throw Boom.notFound(`File not found ${fileId}`);
}
throw err;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment