Skip to content

Commit 163baac

Browse files
committed
Fix S3 putObject with readable streams
They don't work as a content length has to be known.
1 parent 0c72e28 commit 163baac

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

src/components/file/bucket/s3-bucket.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { NoSuchKey, S3 } from '@aws-sdk/client-s3';
22
import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
33
import { Type } from '@nestjs/common';
4+
import { bufferFromStream } from '@seedcompany/common';
45
import { Command } from '@smithy/smithy-client';
56
import { Duration } from 'luxon';
67
import { join } from 'path/posix';
@@ -59,10 +60,18 @@ export class S3Bucket extends FileBucket {
5960
}
6061

6162
async putObject(input: PutObjectInput) {
63+
// S3 needs to know the content length either from body or the header.
64+
// Since we streams don't have that, and we don't know from file, we need to
65+
// buffer it. This way we can know the length to send to S3.
66+
const fixedLengthBody =
67+
input.Body instanceof Readable
68+
? await bufferFromStream(input.Body)
69+
: input.Body;
6270
await this.s3.putObject({
6371
...input,
6472
Key: this.fullKey(input.Key),
6573
Bucket: this.bucket,
74+
Body: fixedLengthBody,
6675
});
6776
}
6877

0 commit comments

Comments
 (0)