Azure 函数不接受在远程创建文件

Azure function don't accept to create file on remote

我会在本地下载文件创建一个流然后发送到 API。

在本地主机中,文件通过 blobClient.downloadToFile(defaultFile);

创建

但是当我部署函数时它找不到要流式传输的文件,所以我认为下载没有发生或下载位置不正确。

我收到这个错误

[Error: ENOENT: no such file or directory, open 'D:\home\site\wwwroot\importPbix\exampleName.pbix'

这是我的代码

const blobServiceClient = BlobServiceClient.fromConnectionString(
  process.env.CONNEXION_STRING
);
const containerClient = blobServiceClient.getContainerClient(
  params.containerName
);
const blobClient = containerClient.getBlobClient(process.env.FILE_LOCATION); // get file from storage
let blobData;
var defaultFile = path.join(params.baseDir, `${params.reportName}.pbix`); // use path module
let stream;

try {
  blobData = await blobClient.downloadToFile(defaultFile);
  console.log(blobData);
  stream = fs.createReadStream(defaultFile);
} catch (error) {
  params.context.log(error);
  console.log(error);
}
var options = {
  method: "POST",
  url: `https://api.powerbi.com/v1.0/myorg/groups/${params.groupId}/imports?datasetDisplayName=${params.reportName}`,
  headers: {
    "Content-Type": "multipart/form-data",
    Authorization: `Bearer ${params.accessToken} `,
  },
  formData: {
    "": {
      value: stream,
      options: {
        filename: `${params.reportName}.pbix`,
        contentType: null,
      },
    },
  },
};
//check if file keep in mem
return new Promise(function (resolve, reject) {
  request(options, function (error, response) {
    if (error) {
      params.context.log(error);

      reject(error);
    } else {
      params.context.log(response);
      resolve(response.body);
    }
    fs.unlinkSync(defaultFile);
  });
});

我发现这个 post 有同样的问题,这就是为什么我使用 path 模块并将 __dirname 传递给函数 params.baseDir

如果你想从 Azure blob 下载文件并将其作为流读取,只需尝试下面的代码,在这个演示中,我尝试将 .txt 文件下载到临时文件夹(你应该先创建它在 Azure 函数上)并从流中打印其内容以进行快速测试:

module.exports = async function (context, req) {
    
    const { BlockBlobClient } = require("@azure/storage-blob")
    const fs = require('fs')
  

    const connStr = '<connection string>'
    const container = 'files'
    const blobName = 'test.txt'
    const tempPath = 'd:/home/temp/'
    const tempFilePath = tempPath + blobName

    const blobClient = new BlockBlobClient(connStr,container,blobName);

    await blobClient.downloadToFile(tempFilePath).then(async function(){
        context.log("download successfully")

        let stream =  fs.createReadStream(tempFilePath)
        //Print text content,just check if stream has been readed successfully
        context.log("text file content:")
        context.log(await streamToString(stream))

        //You can call your API here...
    })

    function streamToString (stream) {
        const chunks = [];
        return new Promise((resolve, reject) => {
          stream.on('data', (chunk) => chunks.push(Buffer.from(chunk)));
          stream.on('error', (err) => reject(err));
          stream.on('end', () => resolve(Buffer.concat(chunks).toString('utf8')));
        })
      }
    
    context.res = {       
        body: 'done'
    }

}

结果

文件已下载:

成功读取为流: