Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在ASP.NET Core 2.1上获取blobs元数据?FetchAttributes方法似乎未实现_C#_Azure_Asp.net Core - Fatal编程技术网

C# 如何在ASP.NET Core 2.1上获取blobs元数据?FetchAttributes方法似乎未实现

C# 如何在ASP.NET Core 2.1上获取blobs元数据?FetchAttributes方法似乎未实现,c#,azure,asp.net-core,C#,Azure,Asp.net Core,我正在尝试从Azure Blob存储中检索一些云对象,我需要访问它们的元数据。在.Net Standard framework上执行此操作时,我可以使用以下方法: blob.FetchAttributes() 但是,这个方法似乎没有在.NETCore2.1上实现,我在文档中找不到任何等效的方法 你有什么解决办法吗 以下是代码(在.Net标准上工作,但在Core上失败): Dictionary dic=newdictionary(); CloudBlobDirectoryDirectory=co

我正在尝试从Azure Blob存储中检索一些云对象,我需要访问它们的元数据。在.Net Standard framework上执行此操作时,我可以使用以下方法:

blob.FetchAttributes()
但是,这个方法似乎没有在.NETCore2.1上实现,我在文档中找不到任何等效的方法

你有什么解决办法吗

以下是代码(在.Net标准上工作,但在Core上失败):

Dictionary dic=newdictionary();
CloudBlobDirectoryDirectory=container.GetDirectoryReference(cloudLink.BlobFolderName);
foreach(目录.ListBlobsSegmentedAsync(null.Result.Results)中的IListBlobItem blobItem)
{
if(blobItem是CloudBlockBlob blob)
{
FetchAttributes();
if(blob.Metadata.ContainsKey(DefaultMetadataKey))
{
if(blob.Metadata.ContainsKey(DefaultMetadataKey))
Add(cloudLink.ReadMeta(blob.Metadata[DefaultMetadataKey]),blob.Uri);
}
}
}

谢谢你的帮助

我认为您使用的是nuget软件包
WindowsAzure.Storage
,该软件包中没有用于dotnet core的同步方法,有关详细信息,请参阅此文件

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

但是现在有了新的nuget包9.4.2版,它支持dotnet core的同步方法(比如
FetchAttributes

请使用新的软件包,带有.NETCore2.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();
        }
    }
}

使用。另外,请注意:使用适当的等待,而不是在
目录中调用.Result.ListBlobsSegmentedAsync(null).Result.Results
谢谢@PeterBons。它就像一个符咒。从您发送给我的文档链接中,您如何确定哪种方法对.NET Core有效,哪种方法无效?通过查看MSDN,我找不到任何合乎逻辑的方法来区分它们。@XavierAM,如果答案对您有效,请帮助将其标记为答案。谢谢。你好@PeterBons。在您谈到使用适当的wait之后,我将异步调用转换为
var listingResult=wait directory.ListBlobsSegmentedAsync(null);foreach(listingResult.Results中的IListBlobItem blobItem){…}
。foreach有没有办法直接在等待的方法上枚举?这是一个细节,但我更喜欢一行而不是两行(我对异步编程还不是很精通),你可以在(wait directory.ListBlobsSegmentedAsync(null)).Results中执行
foreach(IListBlobItem-blobItem-in(wait directory.ListBlobsSegmentedAsync.Results){…}
你是正确的,我使用的是Windows.Storage-Nuget包。我没有意识到这个问题。非常感谢
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();
        }
    }
}