【问题标题】:Check if a blob exist in a path in NodeJS检查 NodeJS 中的路径中是否存在 blob
【发布时间】:2020-11-04 21:24:58
【问题描述】:

假设我想在容器中上传 blob-> azureblob

路径:123/human/a.json

我想检查路径中是否存在任何 blob:123/human/

我找不到任何好的资源。

在c#中找到这个How to check wether a CloudBlobDirectory exists or not?

在节点上找不到任何东西

【问题讨论】:

  • 希望this帮助
  • 您要检查目录中是否存在特定 blob 或目录中的任何 blob?
  • 目录中的任何 blob

标签: node.js azure azure-storage azure-blob-storage


【解决方案1】:

如果您只想检查虚拟目录中是否存在任何 blob,您可以使用 SDK 中的 listBlobsSegmentedWithPrefix 方法并尝试列出 blob。如果您获得的结果计数大于零,则表示目录中存在 blob。例如,看一下示例代码:

blobService.listBlobsSegmentedWithPrefix('azureblob', '123/human/', null, {
  delimiter: '',
  maxReults: 1
}, function(error, result) {
  if (!error) {
    const entries = result.entries;
    if (entries.length > 0) {
      console.log('Blobs exist in directory...');
    } else {
      console.log('No blobs exist in directory...');
    }
  }
});

如果您正在寻找虚拟目录中是否存在特定 blob,您可以简单地使用 SDK 的 doesBlobExist 方法。例如,看一下示例代码:

blobService.doesBlobExist('azureblob', '123/human/a.json', function(error, result) {
  if (!error) {
    if (result.exists) {
      console.log('Blob exists...');
    } else {
      console.log('Blob does not exist...');
    }
  }
});

【讨论】:

  • 你能以同步的方式做到这一点吗?
【解决方案2】:

由于 doesBlobExist 返回一个 Promise,您可以尝试以下实现:

**

export async function doesBlobExist(
  connectionString,
  containerName,
  blobFileName
): Promise<boolean> {
  const promise: Promise<boolean> = new Promise((resolve, reject) => {
    try {
      const blobService = azure.createBlobService(connectionString);
      
      blobService.doesBlobExist(containerName, blobFileName, function (
        error,
        result
      ) {
        if (!error) {
          resolve(result.exists);
        } else {
          reject(error);
        }
      });
    } catch (err) {
      reject(new Error(err));
    }
  });
  return promise;
}

**

【讨论】:

    猜你喜欢
    • 2019-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-05
    • 2011-02-08
    • 2020-05-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多