From a5cb4d3b8fbeff6e6e9939f2759cf842ff4647e4 Mon Sep 17 00:00:00 2001 From: Aditya Patadia Date: Sat, 28 Oct 2023 13:16:16 +0530 Subject: [PATCH] file is streamed rather than buffered to reduce memory usage. --- src/index.ts | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/index.ts b/src/index.ts index 2f40952..1f73fdb 100644 --- a/src/index.ts +++ b/src/index.ts @@ -6,7 +6,7 @@ import { } from '@aws-sdk/client-s3' import StorageBase, { type ReadOptions, type Image } from 'ghost-storage-base' import { join } from 'path' -import { readFile } from 'fs/promises' +import { createReadStream } from 'fs' import type { Readable } from 'stream' import type { Handler } from 'express' @@ -152,10 +152,8 @@ class S3Storage extends StorageBase { async save(image: Image, targetDir?: string) { const directory = targetDir || this.getTargetDir(this.pathPrefix) - const [fileName, file] = await Promise.all([ - this.getUniqueFileName(image, directory), - readFile(image.path), - ]) + const fileName = await this.getUniqueFileName(image, directory); + const file = createReadStream(image.path) let config: PutObjectCommandInput = { ACL: this.acl,