【问题标题】:Corrupted zip after coyping an excel file from Azure Data Lake Storage, using NodeJS使用 NodeJS 从 Azure Data Lake Storage 复制 excel 文件后损坏的 zip
【发布时间】:2021-09-24 18:07:08
【问题描述】:

我正在关注 this tutorial 从我的 Azure Data Lake Storage 中收集一个文件,然后将其临时存储在我的 Azure Function 中,在该文件(这是一个 .xlsx 文件)中执行一些操作以及其他一些操作现在不相关。尝试使用 ExcelJS 库在本地打开下载的文件时出现以下错误:

结果:FailureException:错误:到达数据结尾(数据长度 = 100338,询问索引 = 161705)。损坏的拉链

完整代码如下:

    const Excel = require('exceljs');
    const wb = new Excel.Workbook();
    
    const fs = require('fs');
    
    const path = require('path');
    
    const { StorageSharedKeyCredential, DataLakeServiceClient } = require("@azure/storage-file-datalake");
    
    module.exports = async function (context, req) {
        const accountName = "xx";
        const accountKey = "xx";
     
        // Connect to the storage account
        const datalakeServiceClient = GetDataLakeServiceClient(accountName, accountKey);
    
        // Get the container
        const containerPath = 'my-path';
        const fileSystemClient = datalakeServiceClient.getFileSystemClient(containerPath);
    
        // Obtain the file
        const fileClient = fileSystemClient.getFileClient('my-file.xlsx');
    
        const downloadResponse = await fileClient.read();
    
        const downloaded = await streamToString(downloadResponse.readableStreamBody);
    
        async function streamToString(readableStream) {
            return new Promise((resolve, reject) => {
                const chunks = [];
                readableStream.on("data", (data) => {
                    chunks.push(data.toString());
                });
                readableStream.on("end", () => {
                    resolve(chunks.join(""));
                });
                readableStream.on("error", reject);
            });
        }
    
        // Temporarily create it
        fs.writeFileSync('excel.xlsx', downloaded, function (err) {
            if (err) throw err;
        });
        
        // Proceed with the operations on the excel
        var filePath = path.resolve('/home/site/wwwroot', 'excel.xlsx')
    
        await wb.xlsx.readFile(filePath).then(function() {
            context.log('here2');
            var sh = wb.getWorksheet('Sheet 1');
    
            context.log(sh.getRow(7));
        });
    
        // Delete the temporary file
        fs.unlinkSync('excel.xlsx', function (err) {
            if (err) throw err;
        });
    }
    
    /**
     * Function to create an instance of DataLakeServiceClient
     * @param accountName The name of the storage account
     * @param accountKey Access Key for the storage account
     */
    function GetDataLakeServiceClient(accountName, accountKey) {
    
      const sharedKeyCredential = 
        new StorageSharedKeyCredential(accountName, accountKey);
      
      const datalakeServiceClient = new DataLakeServiceClient(
        `https://${accountName}.dfs.core.windows.net`, sharedKeyCredential);
    
      return datalakeServiceClient;             
    }

如果我导航到路径 /home/site/wwwroot/,该文件存在,但如果我尝试下载并打开它,它会显示该文件存在错误。这意味着它没有从数据湖中正确复制。

任何输入或帮助将不胜感激。非常感谢。

【问题讨论】:

  • Excel 文件是二进制文件,您将它们视为纯文本文件。我相信这就是您的文件损坏的原因。

标签: node.js excel azure azure-functions exceljs


【解决方案1】:

请尝试将您的streamToString 更改为以下内容:

async function streamToString(readableStream) {
    return new Promise((resolve, reject) => {
        let data = Buffer.from([]);
        readableStream.on("data", (dataBuffer) => {
            data = Buffer.concat([data, dataBuffer], data.length + dataBuffer.length);
        });
        readableStream.on("end", () => {
            resolve(data);
        });
        readableStream.on("error", reject);
    });
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-22
    • 1970-01-01
    • 2019-09-10
    • 2018-03-20
    • 2022-11-10
    • 2019-04-01
    • 2018-05-26
    • 1970-01-01
    相关资源
    最近更新 更多