Skip to content

Commit 4322589

Browse files
authored
fix(s3-service/getObject): resolve wrong output type received (#236)
- before this change, we received [object Object] because the AWS SDK returned us a stream instead of a buffer like it used to be in v2, and it was dealt with like a buffer
1 parent 09f195d commit 4322589

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

lib/s3-service.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,23 @@
88
// Require AWS SDK
99
const { S3Client, GetObjectCommand } = require('@aws-sdk/client-s3'); // AWS SDK
1010
const { getSignedUrl } = require('@aws-sdk/s3-request-presigner');
11+
const { streamToBuffer } = require('./utils');
1112

1213
// Export
1314
exports.client = new S3Client();
1415

1516
exports.getObject = (params) => {
16-
const cmd = new GetObjectCommand(params);
17-
const promise = this.client.send(cmd);
1817
return {
19-
promise: () => promise,
18+
promise: async () => {
19+
const res = await this.client.send(new GetObjectCommand(params));
20+
21+
if (!res.Body) return res;
22+
23+
return {
24+
...res,
25+
Body: await streamToBuffer(res.Body),
26+
};
27+
},
2028
};
2129
};
2230

lib/utils.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,3 +201,11 @@ exports.stringifyHeaders = (headers) =>
201201
}),
202202
{}
203203
);
204+
205+
exports.streamToBuffer = (stream) =>
206+
new Promise((resolve, reject) => {
207+
const chunks = [];
208+
stream.on('data', (chunk) => chunks.push(chunk));
209+
stream.on('error', reject);
210+
stream.on('end', () => resolve(Buffer.concat(chunks)));
211+
});

0 commit comments

Comments
 (0)