【发布时间】:2026-01-03 19:55:01
【问题描述】:
我用的是azure文件存储,用express JS写一个后端来渲染存储在azure文件存储中的内容。
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 看看文件客户端类是否有我想要的,但它似乎没有对此有用的方法。
【问题讨论】:
-
只需使用 Bearer 令牌调用此 URL -
https://myaccount.file.core.windows.net/myshare/mydirectorypath/myfile?comp=metadata。如果返回 200 OK,则说明您有文件,否则文件不存在 - docs.microsoft.com/en-us/rest/api/storageservices/…
标签: javascript node.js azure express