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

如何在启用分层命名空间的Azure Blob存储中创建目录?

使用Azure.Storage.Blobs创建启用分层命名空间的Blob Storage目录

启用分层命名空间(ADLS Gen2)后,Blob Storage支持真实目录实体而非虚拟路径。以下是用Azure.Storage.Blobs NuGet包创建目录的具体实现:

核心实现代码

直接通过BlobContainerClient获取目录对应的BlobClient,并指定BlobType.Directory创建:

using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;

// 替换为你的存储账户连接字符串、容器名和目标目录路径
var storageConnectionString = "DefaultEndpointsProtocol=https;AccountName=xxx;AccountKey=xxx;EndpointSuffix=core.windows.net";
var containerName = "sample-container";
var targetDirectoryPath = "docs/reports/2024";

// 初始化容器客户端
var containerClient = new BlobContainerClient(storageConnectionString, containerName);
await containerClient.CreateIfNotExistsAsync();

// 创建真实目录
var directoryClient = containerClient.GetBlobClient(targetDirectoryPath);
var createResult = await directoryClient.CreateIfNotExistsAsync(BlobType.Directory);

if (createResult.Value.Created)
{
    Console.WriteLine($"目录 {targetDirectoryPath} 创建成功");
}
else
{
    Console.WriteLine($"目录 {targetDirectoryPath} 已存在");
}

关键说明

  • 路径格式:使用/作为目录分隔符,无需在路径开头添加/
  • 多级目录:代码支持直接创建多级目录(如docs/reports/2024),无需逐层创建父目录
  • 幂等性:CreateIfNotExistsAsync方法会自动判断目录是否存在,存在则返回已存在状态,不会抛出异常

额外建议

虽然Azure.Storage.Blobs可以实现目录操作,但针对ADLS Gen2场景,更推荐使用Azure.Storage.Files.DataLake NuGet包,它提供了更贴合分层命名空间的API(如DataLakeFileSystemClient.CreateDirectoryAsync),操作更直观。

内容的提问来源于stack exchange,提问作者me.at.coding

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.17 17:30:02