Replace S3 read-store-upload loop with file streams

This no longer buffers file contents in memory and limits the maximum
number of file descriptors open concurrently.

Signed-off-by: Joel Low <joel@joelsplace.sg>
This commit is contained in:
Joel Low
2021-02-26 11:50:35 +08:00
parent 2b40f319c3
commit 61299519fb
3 changed files with 19 additions and 11 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/techdocs-common': patch
---
Remove read-store-upload loop when uploading S3 objects for TechDocs
@@ -99,6 +99,9 @@ describe('AwsS3Publish', () => {
assets: {
'main.css': '',
},
attachments: {
'image.png': Buffer.from([2, 3, 5, 3, 5, 3, 5, 9, 1]),
},
},
});
});
@@ -167,18 +167,18 @@ export class AwsS3Publish implements PublisherBase {
const entityRootDir = `${entity.metadata.namespace}/${entity.kind}/${entity.metadata.name}`;
const destination = `${entityRootDir}/${relativeFilePathPosix}`; // S3 Bucket file relative path
const fileContent = await fs.readFile(filePath, 'utf8');
const params = {
Bucket: this.bucketName,
Key: destination,
Body: fileContent,
};
// Rate limit the concurrent execution of file uploads to batches of 10 (per publish)
const uploadFile = limiter(() =>
this.storageClient.upload(params).promise(),
);
const uploadFile = limiter(() => {
const fileStream = fs.createReadStream(filePath);
const params = {
Bucket: this.bucketName,
Key: destination,
Body: fileStream,
};
return this.storageClient.upload(params).promise();
});
uploadPromises.push(uploadFile);
}
await Promise.all(uploadPromises);