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

如何将文件上传至Azure Blob Storage容器根目录(去除本地路径结构)

问题:如何将本地文件直接上传至Azure Blob Storage容器根目录,而非保留本地路径结构?

现有代码可将指定本地目录localPath下的文件上传至Azure Blob Storage容器,但文件未存放在容器根目录,而是生成了与localPath匹配的文件夹结构。示例本地路径为string localPath = @"C:\example\path\to\files";,上传后容器内结构如下:

. 
+-- example 
| +-- path 
| +-- to 
| +-- files 
| +-- file1.json 
| +-- file2.json 
| +-- file3.json 
| +-- file4.json 
| +-- file5.json 

请问需对下方代码做出哪些修改,才能让文件直接上传至容器根目录而非保留本地路径结构?

原代码(Program.cs)

namespace AzureBlobStorageFileTransfer {
 using Microsoft.Azure.Storage;
 using Microsoft.Azure.Storage.Blob;
 using System;
 using System.IO;
 using System.Threading.Tasks;

 public static class Program {
 public static void Main() {
 ProcessAsync().GetAwaiter().GetResult();
 }

 private static async Task ProcessAsync() {
 CloudStorageAccount storageAccount = null;
 CloudBlobContainer cloudBlobContainer = null;
 string storageConnectionString = Environment.GetEnvironmentVariable("storageconnectionstring");

 if (CloudStorageAccount.TryParse(storageConnectionString, out storageAccount)) {
 try {
 CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient();
 cloudBlobContainer = cloudBlobClient.GetContainerReference("example");

 BlobContainerPermissions permissions = new BlobContainerPermissions {
 PublicAccess = BlobContainerPublicAccessType.Blob
 };
 await cloudBlobContainer.SetPermissionsAsync(permissions);

 string localPath = @"C:\example\path\to\files";
 var txtFiles = Directory.EnumerateFiles(localPath, "*.json");

 foreach (string currentFile in txtFiles) {
 CloudBlockBlob cloudBlockBlob = cloudBlobContainer.GetBlockBlobReference(currentFile);
 await cloudBlockBlob.UploadFromFileAsync(currentFile);
 }
 } catch (StorageException ex) {
 Console.WriteLine("Error returned from the service: {0}", ex.Message);
 }
 } else {
 Console.WriteLine(
 "A connection string has not been defined in the system environment variables. " +
 "Add a environment variable named 'storageconnectionstring' with your storage " +
 "connection string as a value.");
 }
 }
 }
}

解决方案

嘿,这个问题的核心很简单:当前代码把完整的本地文件路径当作了Blob的名称,Azure Blob Storage会把路径里的目录分隔符(比如\)转换成Blob的虚拟目录结构,所以才会出现和本地一样的文件夹层级。

我们只需要修改获取Blob引用的那一行,只传入纯文件名,而不是完整路径。具体步骤如下:

修改要点

  1. 使用Path.GetFileName(currentFile)方法从完整本地路径中提取出纯文件名(比如从C:\example\path\to\files\file1.json得到file1.json)
  2. 把这个纯文件名传给GetBlockBlobReference(),这样文件就会直接上传到容器根目录

修改后的完整代码

namespace AzureBlobStorageFileTransfer {
 using Microsoft.Azure.Storage;
 using Microsoft.Azure.Storage.Blob;
 using System;
 using System.IO;
 using System.Threading.Tasks;

 public static class Program {
 public static void Main() {
 ProcessAsync().GetAwaiter().GetResult();
 }

 private static async Task ProcessAsync() {
 CloudStorageAccount storageAccount = null;
 CloudBlobContainer cloudBlobContainer = null;
 string storageConnectionString = Environment.GetEnvironmentVariable("storageconnectionstring");

 if (CloudStorageAccount.TryParse(storageConnectionString, out storageAccount)) {
 try {
 CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient();
 cloudBlobContainer = cloudBlobClient.GetContainerReference("example");

 BlobContainerPermissions permissions = new BlobContainerPermissions {
 PublicAccess = BlobContainerPublicAccessType.Blob
 };
 await cloudBlobContainer.SetPermissionsAsync(permissions);

 string localPath = @"C:\example\path\to\files";
 var txtFiles = Directory.EnumerateFiles(localPath, "*.json");

 foreach (string currentFile in txtFiles) {
 // 关键修改:只提取文件名作为Blob的名称
 string blobFileName = Path.GetFileName(currentFile);
 CloudBlockBlob cloudBlockBlob = cloudBlobContainer.GetBlockBlobReference(blobFileName);
 await cloudBlockBlob.UploadFromFileAsync(currentFile);
 }
 } catch (StorageException ex) {
 Console.WriteLine("Error returned from the service: {0}", ex.Message);
 }
 } else {
 Console.WriteLine(
 "A connection string has not been defined in the system environment variables. " +
 "Add a environment variable named 'storageconnectionstring' with your storage " +
 "connection string as a value.");
 }
 }
 }
}

验证效果

修改后,上传到容器的结构就会变成:

. 
+-- file1.json 
+-- file2.json 
+-- file3.json 
+-- file4.json 
+-- file5.json 

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 08:08:39