Create a zip file in memory and then store to AWS S3

Issue

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);

Solution

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);

Answered By – Guy Hagemans

Answer Checked By – Cary Denson (AngularFixing Admin)

Leave a Reply

Your email address will not be published.