使用 C# 上传带有标签的 Azure blob

问题描述 投票:0回答:1

长期涉足 C/C# 和 Azure 世界的 VBA 程序员。我有这段代码,可将发票 PDF 上传到 Azure 中的 blob。我上传每个 PDF 时都必须添加标签。我搜索了一周,但找不到代码来完成我认为简单的事情。

如有任何建议,我们将不胜感激。

谢谢。

    using Azure.Storage.Blobs;
    using Azure.Storage.Blobs.Models;
    using System.Diagnostics;
    using System.IO;
    using System.Reflection.Metadata;
    using static System.Net.WebRequestMethods;
    using static System.Windows.Forms.Design.AxImporter;

    namespace UploadDealerDocuments
    {
    class program
    {
    static string connectionString =         "DefaultEndpointsProtocol=https;AccountName=###;AccountKey=###;EndpointSuffix=core.windows.net";
    static string containerName = "smi-statements-invoices-staging";
    static void Main(string[] args)
    {
    BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);
    BlobContainerClient containerClient = blobServiceClient.CreateBlobContainer(containerName);
    string str = "";
    var files = Directory.GetFiles(@"N:\Azure\invoices\");
    foreach (var file in files)
    {
    str = Path.GetFileName(file);
    str = str.Replace("_", "/");
    Console.WriteLine(str);
    using (MemoryStream stream = new MemoryStream(System.IO.File.ReadAllBytes(file)))
    {
    containerClient.UploadBlob(str, stream);
    }
    str = @"N:\Azure\Invoices\Uploaded\" + Path.GetFileName(file);
    System.IO.File.Move(@"N:\Azure\Invoices\" + Path.GetFileName(file), str);
    Console.WriteLine(file + "Uploaded!");
    }
    Console.WriteLine("Files Uploaded!");
    Console.Read();
    }
    }
    }

我上传 PDF 时必须添加标签。像这样的东西。

    var Tags = new Dictionary<string, string> {
    { "file_date", "2022-10-03" },
    { "title", "some title" }
    };


_ = await blobClient.UploadAsync(str, options);

我尝试过这段代码,它在await语句上出错,并且containerClient.UploadBlob语句没有选项参数。

BlobUploadOptions options = new BlobUploadOptions
    {
        Tags = new Dictionary<string, string> {
            { "name", "myBlob" },
            { "type", "blobTrigger" },
            { "direction", "in" },
            { "path", "path1/path2/name" },
            { "connection", "blob_STORAGE" }
        }
    };

    _ = await blobClient.UploadAsync(str, options);
c# azure-blob-storage
1个回答
1
投票

尝试一下 https://learn.microsoft.com/en-us/rest/api/storageservices/set-blob-tags

具体来说,您应该能够使用 blobClient.SetTags(myDict) 传递标签字典

© www.soinside.com 2019 - 2024. All rights reserved.