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

使用C#上传带标签的Azure Blob的技术求助

解决方案:Azure Blob上传时添加标签

问题分析

你遇到的两个核心问题:

  • await 报错:当前Main是同步方法,无法直接使用异步关键字await
  • containerClient.UploadBlob无options参数:该方法确实没有支持设置标签的重载,需要通过BlobClient来操作

修改后的完整代码

using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;
using System.IO;
using System.Collections.Generic;

namespace UploadDealerDocuments
{
    class Program
    {
        static string connectionString = "DefaultEndpointsProtocol=https;AccountName=###;AccountKey=###;EndpointSuffix=core.windows.net";
        static string containerName = "smi-statements-invoices-staging";

        // 改为异步Main方法,支持await关键字
        static async System.Threading.Tasks.Task Main(string[] args)
        {
            BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);
            // 用GetBlobContainerClient替代CreateBlobContainer,避免容器已存在时报错
            BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);
            // 确保容器存在(不存在则创建)
            await containerClient.CreateIfNotExistsAsync();

            var files = Directory.GetFiles(@"N:\Azure\invoices\");
            foreach (var file in files)
            {
                string blobName = Path.GetFileName(file).Replace("_", "/");
                Console.WriteLine($"准备上传:{blobName}");

                // 获取对应Blob的专属客户端实例
                BlobClient blobClient = containerClient.GetBlobClient(blobName);

                // 构造上传选项,设置自定义标签
                BlobUploadOptions uploadOptions = new BlobUploadOptions
                {
                    Tags = new Dictionary<string, string>
                    {
                        // 可根据文件动态设置标签,示例值可替换为实际业务数据
                        { "file_date", "2022-10-03" },
                        { "title", Path.GetFileNameWithoutExtension(file) },
                        { "file_type", "pdf" }
                    }
                };

                // 读取文件流并上传
                using (FileStream stream = File.OpenRead(file))
                {
                    await blobClient.UploadAsync(stream, uploadOptions);
                }

                // 移动已上传文件到目标目录
                string destPath = Path.Combine(@"N:\Azure\Invoices\Uploaded", Path.GetFileName(file));
                File.Move(file, destPath);
                Console.WriteLine($"{file} 上传完成!");
            }

            Console.WriteLine("所有文件上传完成!");
            Console.Read();
        }
    }
}

关键修改说明

  1. 异步Main方法:将static void Main改为static async Task Main,合法支持await调用异步上传接口。
  2. 切换到BlobClient操作:BlobContainerClient.UploadBlob无上传选项重载,改用containerClient.GetBlobClient(blobName)获取Blob专属客户端,其UploadAsync方法支持传入BlobUploadOptions设置标签。
  3. 容器初始化优化:用GetBlobContainerClient+CreateIfNotExistsAsync组合,避免容器已存在时抛出异常。
  4. 流操作优化:直接用File.OpenRead打开文件流,比先读入字节再转MemoryStream更节省内存。

额外提示

  • 若需从文件名或文件内容动态提取标签(如发票日期),可在循环内添加解析逻辑,替换示例中的固定标签值。
  • 确保你的Azure.Storage.Blobs NuGet包为最新版本,旧版本可能缺失部分API。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.13 16:40:40