【问题标题】:Is there a way to filter listBlobFlatSegment on azure blob storage by the end of their names?有没有办法在名称末尾过滤 azure blob 存储上的 listBlobFlatSegment ?
【发布时间】:2020-05-03 18:16:41
【问题描述】:

我正在尝试获取容器上 blob 列表的 blob 名称,该容器是最新的并且以类似“_20167”之类的内容结尾,所有文件都是 XML 类型并具有以下结构:“ {UUID }_{DocumentType}_{Status}_{Source}_{CompanyNumber}.xml”,例如:“1CE6A613-3D64-40E9-B17F-68C063ABC613_2C98EF3B-038B-4AA4-91E1-6FC32E012974_factura_vigente.xml”。

我正在使用 containerURL.listBlobFlatSegment 方法的前缀,但我找不到更改功能以使用前缀搜索结尾的方法。 这是我的函数代码,我只传递要搜索的容器名称和de参数,这称为“companyNumber”:

async downloadListOfBlobs({ containerName, companyNumber }) {
    try {
      const containerURL = ContainerURL.fromServiceURL(
        this._serviceURL,
        containerName
      );
      let marker;
      marker = undefined;
      let blobsInContainer = [];
      const prefix = `${companyNumber}.xml`;
      do {
        const listBlobsResponse = await containerURL.listBlobFlatSegment(
          Aborter.none,
          marker,
          { include: null,
          maxresults: marker , 
          prefix }
        );

        marker = listBlobsResponse.nextMarker;
        for (const blob of listBlobsResponse.segment.blobItems) {
          console.log(`Blob: ${blob.name}`);
          blobsInContainer.push(`${blob.name}`);
        }
      } while (marker);
      return blobsInContainer[0] ? blobsInContainer[0] : '';
    } catch (error) {
      console.log(error);
      throw error;
    }
  }

有人搜索了另一种方法来动态搜索完整的 blob 列表,但我没有获得足够的信息。 非常感谢您的指导!

【问题讨论】:

  • 您可以使用下划线分割文件名,并在数组的第 4 位搜索公司编号。参考:developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
  • 非常感谢,考虑到您的想法,我可以分离出我感兴趣的 blob 的完整 blob 列表,但是,我仍然不能直接过滤 lisBlobFlatSegment 函数,只有结尾等于我的文档公司编号,这种情况危及性能功能。这是完整的新代码:

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


【解决方案1】:

非常感谢您的回复,考虑到您的想法,我可以分离出我感兴趣的 blob 的完整 blob 列表,但是,我仍然无法直接过滤 lisBlobFlatSegment 函数,只能过滤结尾等于我公司编号的文档, 这种情况危及性能功能。这是完整的新代码:

    try {
      const containerURL = ContainerURL.fromServiceURL(
        this._serviceURL,
        containerName
      );
      let marker;
      marker = undefined;
      let blobsInContainer = [];
      let blobsInContainerNames = [];
      const prefix = `${companyNumber}.xml`;
      do {
          const listBlobsResponse = await containerURL.listBlobFlatSegment(
            Aborter.none,
            marker,
          );
          marker = listBlobsResponse.nextMarker;
          for (const blob of listBlobsResponse.segment.blobItems) {
            if ((prefix && blob.name.endsWith(prefix)) || !prefix) {
              blobsInContainer.push(blob);
            }
          }
        } while (marker);
        blobsInContainer.sort(function(a,b){
          return new Date(b.properties.lastModified) - new Date(a.properties.lastModified);
        })
        for (const blob of blobsInContainer) {
          if ((prefix && blob.name.endsWith(prefix)) || !prefix) {
            blobsInContainerNames.push(blob.name);
          }
        }
        console.log('blobSelected', blobsInContainerNames[0]);
        return blobsInContainerNames[0] ? blobsInContainerNames[0] : '';

      } catch (error) {
        console.log(error);
        throw error;
      }
    }```

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-23
    • 2021-11-08
    • 2012-12-26
    • 2020-02-24
    • 2020-02-26
    • 2020-08-15
    • 1970-01-01
    • 2019-03-03
    相关资源
    最近更新 更多