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

如何用Node.js检查Azure Blob容器中软删除文件的存在性

检测Azure Blob存储中软删除文件的存在性

1. 使用azure-storage库检测软删除文件

doesBlobExist默认仅检测活跃状态的Blob,要包含软删除的Blob,只需在调用时传入includeDeleted: true的配置选项:

const blobService = azure.createBlobService(connectingString);

blobService.doesBlobExist(
  container,
  "folder1/folder2/file.txt",
  { includeDeleted: true }, // 关键配置:纳入软删除Blob的检测范围
  (error, result) => {
    if (error) {
      console.error("检测失败:", error);
      return;
    }
    if (result.exists) {
      console.log("文件存在");
      if (result.deleted) {
        console.log("该文件处于软删除状态");
      }
    } else {
      console.log("文件不存在");
    }
  }
);

返回的result.deleted属性会明确标记该Blob是否处于软删除状态。

2. 使用@azure/storage-blob库检测指定Blob

无需纠结过多实体,核心是通过BlobClient定位目标文件并完成检测,代码示例如下:

const { BlobServiceClient } = require("@azure/storage-blob");

async function checkBlobExists(connectionString, containerName, blobPath) {
  // 初始化Blob服务客户端
  const blobServiceClient = BlobServiceClient.fromConnectionString(connectionString);
  // 获取目标容器的客户端
  const containerClient = blobServiceClient.getContainerClient(containerName);
  // 直接创建对应Blob路径的客户端(传入完整路径"folder1/folder2/file.txt")
  const blobClient = containerClient.getBlobClient(blobPath);

  // 检测Blob存在性,包含软删除状态的Blob
  const exists = await blobClient.exists({ includeDeleted: true });
  
  if (exists) {
    console.log("文件存在");
    // 验证是否为软删除状态
    const props = await blobClient.getProperties({ includeDeleted: true });
    if (props.deleted) {
      console.log("该文件处于软删除状态");
    }
  } else {
    console.log("文件不存在");
  }
}

// 调用示例
checkBlobExists(connectingString, "your-container-name", "folder1/folder2/file.txt")
  .catch(err => console.error(err));

核心说明:

  • getBlobClient方法直接接受完整的Blob层级路径,无需单独处理目录结构。
  • exists和getProperties方法通过includeDeleted: true参数,即可将软删除状态的Blob纳入检测范围。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.09 22:45:32