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

如何将文件上传至Azure Blob容器内的指定文件夹?

Azure Blob上传到容器指定文件夹的代码调整方案

核心原理

Azure Blob 存储本身不存在真实的文件系统层级结构,容器内的「文件夹」是通过Blob名称的路径前缀模拟实现的,只需要在Blob名称前拼接以/分隔的路径,即可上传到对应模拟目录下。

调整步骤

  • 定义你需要上传到容器内的目标路径,将其作为前缀拼接到原文件名前,再传入GetBlobClient方法即可。
  • 路径分隔符统一使用/即可,无需适配本地系统的路径分隔符。

修改后代码示例

以下示例假设你需要上传到容器内的/business/ra_records/目录下,你可以自行替换为实际需要的目标路径:

var connectionString = "xxxxxx";
string containerName = "projects";
var serviceClient = new BlobServiceClient(connectionString);
var containerClient = serviceClient.GetBlobContainerClient(containerName);
var path2 = @"C:\2021\BlobContainer\";
var fileName2 = dados.after.NR_RA + "_" + DateTime.Now.ToString("ddmmyyyyHHmm") + ".txt";
var localFile = Path.Combine(path2, fileName2);

// 核心改动:拼接容器内目标路径到Blob名称前
string targetDirectory = "business/ra_records/";
string fullBlobName = targetDirectory + fileName2;
var blobClient = containerClient.GetBlobClient(fullBlobName);

Console.WriteLine("Uploading to Blob storage");
using FileStream uploadFileStream = File.OpenRead(localFile);
await blobClient.UploadAsync(uploadFileStream, true);
uploadFileStream.Close();

额外提示

注意你原有代码中DateTime.Now.ToString("ddmmyyyyHHmm")里的小写mm代表分钟,如果你需要的是月份参数,请替换为大写的MM,避免文件命名不符合预期。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.23 22:06:05