You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Node.js获取Blob版本ID列表时遇ReferenceError错误求助

问题:Node.js获取Blob版本ID列表时出现ReferenceError: inlcudeVersions is not defined错误

尝试使用Node.js获取Blob版本ID列表,编写代码如下:

async function getBlobNameAndVersions() {
    
  for await (const blob of containerClient.listBlobsFlat(includeVersions?: true)) {
    
    const tempBlockBlobClient = containerClient.getBlockBlobClient(blob.name);

    console.log(`\n\tname: ${blob.versionId}\n\tURL: ${tempBlockBlobClient.name}\n`);

  }

}

getBlobNameAndVersions()

运行后收到错误提示:ReferenceError: inlcudeVersions is not defined。

查看listBlobsFlat的参考文档,其定义为:

listBlobsFlat(options?: ContainerListBlobsOptions): PagedAsyncIterableIterator<BlobItem, ContainerListBlobFlatSegmentResponse>;
/**
 * Returns an AsyncIterableIterator for ContainerListBlobHierarchySegmentResponse
 *
 * @param delimiter - The character or string used to define the virtual hierarchy
 * @param marker - A string value that identifies the portion of
 *                          the list of blobs to be returned with the next listing operation. The
 *                          operation returns the ContinuationToken value within the response body if the
 *                          listing operation did not return all blobs remaining to be listed
 *                          with the current page. The ContinuationToken value can be used as the value for
 *                          the marker parameter in a subsequent call to request the next page of list
 *                          items. The marker value is opaque to the client.
 * @param options - Options to list blobs operation. 

且ContainerListBlobsOptions接口包含includeVersions选项:

export declare interface ContainerListBlobsOptions extends CommonOptions {
     * Specifies whether versions should be included in the enumeration. Versions are listed from oldest to newest in the response.
     */
    includeVersions?: boolean;
}

解决方法

1. 修复参数传递语法错误

listBlobsFlat接收的是一个可选的配置对象,includeVersions?: true是TypeScript接口的定义语法,不能直接用于函数调用。正确做法是传入包含includeVersions属性的对象。

错误提示中的inlcudeVersions是拼写笔误(少了一个c),但核心问题是参数传递方式错误。

2. 修正输出逻辑错误

原代码输出项对应关系错误:

  • blob.versionId是版本ID,需对应"版本ID"输出
  • tempBlockBlobClient.name是Blob名称,tempBlockBlobClient.url才是Blob的完整访问URL

修改后的完整代码

async function getBlobNameAndVersions() {
  // 传入配置对象开启版本枚举
  for await (const blob of containerClient.listBlobsFlat({ includeVersions: true })) {
    const tempBlockBlobClient = containerClient.getBlockBlobClient(blob.name);
    // 修正输出内容,明确区分各字段含义
    console.log(`
      Blob名称: ${blob.name}
      版本ID: ${blob.versionId}
      Blob访问URL: ${tempBlockBlobClient.url}
    `);
  }
}

getBlobNameAndVersions();

关键注意点

  • 需确保目标存储容器已开启版本控制,否则blob.versionId会返回undefined
  • listBlobsFlat({ includeVersions: true })会返回所有Blob的历史版本,按从旧到新的顺序排列

内容的提问来源于stack exchange,提问作者Anil

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 08:55:17