From ffcb11c0c18be43c204903ef0823001e12e17d03 Mon Sep 17 00:00:00 2001 From: Boris Date: Wed, 8 Jan 2025 17:21:27 +0800 Subject: [PATCH] fix: s3 adapter file upload by file path --- .../src/features/attachments/plugins/s3.ts | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/apps/nestjs-backend/src/features/attachments/plugins/s3.ts b/apps/nestjs-backend/src/features/attachments/plugins/s3.ts index a26d8a742d..eeb8ce5c4d 100644 --- a/apps/nestjs-backend/src/features/attachments/plugins/s3.ts +++ b/apps/nestjs-backend/src/features/attachments/plugins/s3.ts @@ -156,7 +156,6 @@ export class S3Storage implements StorageAdapter { const command = new GetObjectCommand({ Bucket: bucket, Key: path, - ResponseContentType: respHeaders?.['Content-Type'], ResponseContentDisposition: respHeaders?.['Content-Disposition'], }); @@ -170,10 +169,11 @@ export class S3Storage implements StorageAdapter { filePath: string, metadata: Record ) { + const readStream = fse.createReadStream(filePath); const command = new PutObjectCommand({ Bucket: bucket, Key: path, - Body: filePath, + Body: readStream, ContentType: metadata['Content-Type'] as string, ContentLength: metadata['Content-Length'] as number, ContentDisposition: metadata['Content-Disposition'] as string, @@ -181,11 +181,16 @@ export class S3Storage implements StorageAdapter { ContentLanguage: metadata['Content-Language'] as string, ContentMD5: metadata['Content-MD5'] as string, }); - - return this.s3Client.send(command).then((res) => ({ - hash: res.ETag!, - path, - })); + return this.s3Client + .send(command) + .then((res) => ({ + hash: res.ETag!, + path, + })) + .finally(() => { + readStream.removeAllListeners(); + readStream.destroy(); + }); } uploadFile(