I'm upgrading to node 18. One of the packages is exceljs. It didn't work, so I'm trying with @nbelyh/exceljs instead.
The code creates an excel document and uploads to an AWS S3 bucket. When running the code locally, it works fine (The file is uploaded to S3 correctly)
But after pushing the code to AWS and running it from serverless lambda it produces a corrupt excel file. It looks like it's a little smaller. The size of the working file produced is 7k and the corrupted file is 6.7k or similar.
This is the essential part of the code.
export async function generateExcelReport(data, {sync, tempFile, ...options}) {
const tempFileReturn = await writePublicTempFile(tempFile || (options.title+'.xlsx'), async stream => {
await writeXLSXFromArrayToStream(data, options, stream);
});
return tempFileReturn.URL;
}
}
async function writeXLSXFromArraysToStream(sheets, stream) {
const workbook = createWorkBook();
for (const {data, options} of sheets) {
addWorkSheet(workbook, data, options).commit();
}
await workbook.commit();
workbook.stream.pipe(stream);
}
import { S3Client, GetObjectCommand, PutObjectCommand } from '@aws-sdk/client-s3';
import { Upload } from '@aws-sdk/lib-storage';
export async function writePublicTempFile(filename, writer) {
const tempFileInfo = typeof filename === 'object' ? filename : await getPublicTempFileInfo(filename);
const writeStream = new PassThrough();
const params = {
Bucket: tempFileInfo.S3Bucket,
Key: tempFileInfo.S3Key,
Body: writeStream,
ContentType: getFileMIME(tempFileInfo.filename), //'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
};
const upload = new Upload({
client: s3,
params
});
await writer(writeStream);
await upload.done();
return tempFileInfo;
}
Any ideas?
Tried different buckets, no luck. Tried buffer instead of stream with some luck, but not fully tested yet.