簡體   English   中英

如何查找 Azure 文件是否存在於 NodeJS

[英]How to find if Azure File exists on NodeJS

我使用的是azure文件存儲,並使用express JS寫了一個后端來渲染azure文件存儲中存儲的內容。

我正在編寫基於https://learn.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://learn.microsoft.com/en-us/javascript/api/@azure/storage-file-share/shareserviceclient?view=azure-node-latest看看文件客戶端 class 是否有我想要的,但它似乎沒有對此有用的方法。

如果您使用的是@azure/storage-file-share (version 12.x)節點 package, ShareFileClient中有一個exists方法。 您可以使用它來查找文件是否存在。 就像是:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM