如何查找Azure文件是否存在于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/shareservicecliclient ?view=azure-node-latest看看文件客户端类是否有我想要的,但它似乎没有有用的方法。

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

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

相关内容

最新更新