Created
August 27, 2019 01:18
-
-
Save skabbes/3f7dba1e1c9ea422c6d14df3d2163932 to your computer and use it in GitHub Desktop.
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
/* 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