c# - 如何获取 ASP.NET Core 2.1 上的 blob 元数据? FetchAttributes 方法看起来没有实现

标签 c# azure asp.net-core

我正在尝试从 Azure Blob 存储检索一些云对象,并且需要访问它们的元数据。在.Net Standard框架上这样做时,我可以使用以下方法:

blob.FetchAttributes()

但是,这个方法似乎没有在 .Net Core 2.1 上实现,我在文档中找不到任何等效的方法。

你有什么解决办法吗?

这里是代码(在.Net Standard上工作但在Core上失败):

Dictionary<T, Uri> dic = new Dictionary<T, Uri>();

CloudBlobDirectory directory = container.GetDirectoryReference(cloudLink.BlobFolderName);

foreach (IListBlobItem blobItem in directory.ListBlobsSegmentedAsync(null).Result.Results)
{
    if (blobItem is CloudBlockBlob blob)
    {
        blob.FetchAttributes();

        if (blob.Metadata.ContainsKey(DefaultMetadataKey))
        {
            if (blob.Metadata.ContainsKey(DefaultMetadataKey))
                dic.Add(cloudLink.ReadMeta(blob.Metadata[DefaultMetadataKey]), blob.Uri);
        }
    }
}

感谢您的帮助!

最佳答案

我认为您使用的是nuget包WindowsAzure.Storage,并且在该包中,没有dotnet core的同步方法,您可以引用这个github issue了解更多详情。

因此,如果您使用上述 nuget 包,则应该使用像 FetchAttributesAsync 这样的异步方法,而不是 FetchAttributes

但是现在有新的 nuget 包 Microsoft.Azure.Storage.Blob ,版本 9.4.2,它支持 dotnet core 的同步方法(如 FetchAttributes)。

请使用新包,.net core 2.1 的示例代码在我这边工作得很好。

using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using System;

namespace ConsoleApp4netcore
{
    class Program
    {
        static void Main(string[] args)
        {
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse("xxxxx");

            var blobClient = storageAccount.CreateCloudBlobClient();
            CloudBlobContainer cloudBlobContainer = blobClient.GetContainerReference("test-1");
            CloudBlobDirectory directory = cloudBlobContainer.GetDirectoryReference("sub1");
            foreach (IListBlobItem blobItem in directory.ListBlobsSegmentedAsync(null).Result.Results)
            {
                if (blobItem is CloudBlockBlob blob)
                { 
                    //the new package supports syncronous method
                    blob.FetchAttributes();

                    foreach (var metadataItem in blob.Metadata)
                    {
                    Console.WriteLine("\tKey: {0}", metadataItem.Key);
                    Console.WriteLine("\tValue: {0}", metadataItem.Value);
                    }
                }

            }            

            Console.ReadLine();
        }
    }
}

关于c# - 如何获取 ASP.NET Core 2.1 上的 blob 元数据? FetchAttributes 方法看起来没有实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55308807/

相关文章:

C# 窗体 : How to set the Base Color of a TabControl (not the tabpage) AND use an icon

c# - RichTextBox 选择同一单词的多次出现

azure - Microsoft azure 帐户登录问题

c# - .net 核心应用程序目标 Linux 上的 .net framework 4.5.2

c# - 迁移到 ASP.Net Core MVC 时,JSON 序列化/反序列化不起作用

c# - 如何从 OKTA 获得用于单元/集成测试的 SAML 响应

c# - C# 中完整的老式 TUI 命令行应用程序

c# - 为什么打开 Couchbase 存储桶会非常慢?

c# - 如何知道 Azure 文件存储共享何时已被删除?

visual-studio-2015 - MVC 6 : System. IO.FileNotFoundException : Could not load file or assembly 'System. Diagnostics.DiagnosticSource