简体   繁体   English

将 csv 文件从 s3 读入 ExcelJS 工作簿

[英]Reading csv file from s3 into ExcelJS Workbook

I want to do some operations on a csv file saved in s3 using exceljs library in my aws lambda. I am not able to initialize the exceljs Workbook object with the response returned by aws-sdk.我想使用 aws lambda 中的 exceljs 库对保存在 s3 中的 csv 文件执行一些操作。我无法使用 aws-sdk 返回的响应初始化 exceljs 工作簿 object。 As I explain below, the issue is mainly that the sdk gives stream of the MDN form whereas exceljs requires nodejs type of stream.正如我在下面解释的,问题主要是 sdk 给出了MDN形式的 stream,而 exceljs 需要 stream 的 nodejs 类型。

The sdk response is like this sdk 响应是这样的
const data: GetObjectCommandOutput = await s3Client.send(command)

data.Body is of type SdkStream<Readable | ReadableStream<any> | Blob | undefined> | undefined data.Body的类型为SdkStream<Readable | ReadableStream<any> | Blob | undefined> | undefined SdkStream<Readable | ReadableStream<any> | Blob | undefined> | undefined SdkStream<Readable | ReadableStream<any> | Blob | undefined> | undefined Here ReadableStream<any> is the MDN kind of stream. SdkStream<Readable | ReadableStream<any> | Blob | undefined> | undefined这里的ReadableStream<any>是 MDN 类型的 stream。

I tried to create a nodejs stream like this(ignoring null check for now) const readableStream: ReadableStream = data.Body.;transformToWebStream();我试图像这样创建一个 nodejs stream(暂时忽略 null 检查) const readableStream: ReadableStream = data.Body.;transformToWebStream();

const expectedNodejsStream = new Readable().wrap(readableStream);

But seems the issue here is wrap works for NodeJS .ReadableStream.但似乎这里的问题是NodeJS .ReadableStream 的wrap工作。

I also tried this as suggested in many answers我也按照许多答案中的建议尝试了这个

const nodeStream = new stream.Writable();
(data.Body!as Readable).pipe(nodeStream);

But an exception is thrown as data.Body does not get typecasted to Readable during runtime.但是会抛出一个异常,因为 data.Body 在运行时不会被类型转换为 Readable。

Exceljs constructor looks like this Exceljs 构造函数看起来像这样

const wb = new Excel.Workbook();
wb.xlsx.read(nodeStream);//doesnt work

or like this或者像这样

new Excel.stream.xlsx.WorkbookReader(data.Body!.transformToWebStream());//does not work

Is it possible to create the excel workbook from sdk response data without having to convert the MDN stream to nodejs stream?是否可以从 sdk 响应数据创建 excel 工作簿,而无需将 MDN stream 转换为 nodejs stream? If no, is there a way to do this conversion without implementing our own stream as suggested in this answer如果不是,是否有一种方法可以在不按照此答案中的建议实施我们自己的 stream 的情况下进行此转换

aws-sdk/client-s3 v^3.121 aws-sdk/client-s3 v^3.121
aws-sdk v2.1158 aws-sdk v2.1158

data.Body can be typecasted to Buffer which is a nodejs construct. data.Body可以类型转换为Buffer ,这是一个 nodejs 构造。 We can create a nodejs Readable stream from this buffer.我们可以从这个缓冲区创建一个 nodejs Readable stream。

const data = await s3Client2.getObject(bucketParams).promise();
  console.log(`data: GetObjectCommandOutput`);
  const dataBuffer = Buffer.from(data.Body as Buffer);

  const wb = new Excel.Workbook();
  wb.csv.read(Readable.from(dataBuffer));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM