【问题标题】:How to find if Azure File exists on NodeJS如何查找 NodeJS 上是否存在 Azure 文件
【发布时间】:2026-01-03 19:55:01
【问题描述】:

我用的是azure文件存储,用express JS写一个后端来渲染存储在azure文件存储中的内容。

我正在编写基于https://docs.microsoft.com/en-us/javascript/api/@azure/storage-file-share/shareserviceclient?view=azure-node-latest的代码

const { ShareServiceClient, StorageSharedKeyCredential } = require("@azure/storage-file-share");

const account = "<account>";
const accountKey = "<accountkey>";

const credential = new StorageSharedKeyCredential(account, accountKey);
const serviceClient = new ShareServiceClient(
  `https://${account}.file.core.windows.net`,
  credential
);

const shareName = "<share name>";
const fileName = "<file name>";

// [Node.js only] A helper method used to read a Node.js readable stream into a Buffer
async function streamToBuffer(readableStream) {
  return new Promise((resolve, reject) => {
    const chunks = [];
    readableStream.on("data", (data) => {
      chunks.push(data instanceof Buffer ? data : Buffer.from(data));
    });
    readableStream.on("end", () => {
      resolve(Buffer.concat(chunks));
    });
    readableStream.on("error", reject);
  });
}

并且可以通过

查看内容
const downloadFileResponse = await fileClient.download();
const output = await streamToBuffer(downloadFileResponse.readableStreamBody)).toString()

问题是,我只想查找文件是否存在,而不是花时间下载整个文件,我该怎么做?

我看了https://docs.microsoft.com/en-us/javascript/api/@azure/storage-file-share/shareserviceclient?view=azure-node-latest 看看文件客户端类是否有我想要的,但它似乎没有对此有用的方法。

【问题讨论】:

标签: javascript node.js azure express


【解决方案1】:

如果你使用@azure/storage-file-share (version 12.x) Node 包,ShareFileClient 中有一个exists 方法。您可以使用它来查找文件是否存在。比如:

const fileExists = await fileClient.exists();//returns true or false.

【讨论】: