AWS s3 V3 Javascript SDK stream file from bucket (GetObjectCommand)
May 3
I've looked all over AWS docs and stack overflow (even went to page 4 of google!!!) but I could not for the life of me work out how to stream a file from S3. The docs for V3 are pretty useless and all the examples I find are from V2.
The send command that V3 uses only returns a promise so how do I get a stream and pipe it instead of waiting for the whole file (it needs to be piped into encryption algo then to a response stream)
this.s3.send(
new GetObjectCommand({
Bucket: '...',
Key: key,
}),
);
I was able to upload fine by passing the stream as the body, is there something I have to do similar here?
uploadToAws(key) {
const pass = new PassThrough();
return {
writeStream: pass,
promise: this.s3.send(
new PutObjectCommand({
Bucket: '...',
Key: key,
Body: pass,
ServerSideEncryption: '...',
ContentLength: 37,
}),
),
};
}
1 answer
Accepted answer · original discussion
May 3
Body from the GetObjectCommand is a readable stream (https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-s3/interfaces/getobjectcommandoutput.html#body).
So you can do:
const command = new GetObjectCommand({
Bucket
Key,
});
const item = await s3Client.send(command);
item.Body.pipe(createWriteStream(fileName));
1 question comment
Use comments to ask for clarification. Post a solution as an answer.
Jun 29