Home > Software engineering >  Create a zip file in memory and then store to AWS S3
Create a zip file in memory and then store to AWS S3

Time:04-24

I'm trying to create a zip file in memory (using TypeScript) and then store it in an AWS S3 bucket. The input is a plain text CSV string (data).

I'm not sure what I'm doing wrong as the zip file that is created in s3 is unreadable when I download the file.

The code:

const zip = new AdmZip();
zip.addFile('tmp.txt', Buffer.from(data, 'utf-8'));
const zipData = await zip.toBufferPromise();
try {
  const now = dayjs().format('YYYYMMDDTHHmmss');
  const fileName = `Feedback-${now}`;
  await this._storageService.saveFile(zipData.toString('binary'), 'sprintFeedback', fileName);

CodePudding user response:

The solution was near and simple: Just send the outcome of the following line to your s3 bucket and it's stored as a proper zip file;

const zipData: Buffer = await zip.toBufferPromise();

const command = new PutObjectCommand({
  Bucket: bucketName,
  Body: zipData,
  Key: fileName,
});

await this._s3Client.send(command);
  • Related