Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/265.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# Azure存储SDK-检查Blob是否为分层存储中的目录_C#_.net Core_Azure Storage - Fatal编程技术网

C# Azure存储SDK-检查Blob是否为分层存储中的目录

C# Azure存储SDK-检查Blob是否为分层存储中的目录,c#,.net-core,azure-storage,C#,.net Core,Azure Storage,我有一个启用了分层存储的Azure存储帐户。我得到一个容器中的blob列表,并试图确定一个特定blob是否是一个目录 我让它在REST API客户端中工作,如下所示: public async Task<List<StoragePath>> ListPathsAsync(string filesystem) { string uri = $"https://{accountName}.{dnsSuffix}/{filesystem}?recursive=true&

我有一个启用了分层存储的Azure存储帐户。我得到一个容器中的blob列表,并试图确定一个特定blob是否是一个目录

我让它在REST API客户端中工作,如下所示:

public async Task<List<StoragePath>> ListPathsAsync(string filesystem)
{
    string uri = $"https://{accountName}.{dnsSuffix}/{filesystem}?recursive=true&resource=filesystem";
    var result = await SendRequestAsync<StoragePathList>(HttpMethod.Get, uri, CancellationToken.None, true);
    return result.Content.Paths?.ToList();
}

public class StoragePath
{
    [JsonProperty(PropertyName = "isDirectory")]
    public bool IsDirectory { get; set; }

    // other properties
}
public class StoragePath
{
    public bool IsDirectory { get; set; }
    public string Name { get; set; }
}
public异步任务ListPathsAsync(字符串文件系统)
{
字符串uri=$“https://{accountName}.{dnsSuffix}/{filesystem}?recursive=true&resource=filesystem”;
var result=await SendRequestAsync(HttpMethod.Get,uri,CancellationToken.None,true);
返回result.Content.path?.ToList();
}
公共类存储路径
{
[JsonProperty(PropertyName=“isDirectory”)]
公共bool IsDirectory{get;set;}
//其他属性
}
SendRequestAsync
方法仅使用JsonConvert对API响应的内容进行反序列化

var result = JsonConvert.DeserializeObject<T>(await response.Content.ReadAsStringAsync());
var result=JsonConvert.DeserializeObject(wait response.Content.ReadAsStringAsync());
现在我正试图弄清楚如何使用.NETSDK做同样的事情,但我在任何地方都看不到IsDirectory属性

public async Task<List<StoragePath>> ListPathsAsync(string filesystem)
    {
        var containerClient = new BlobContainerClient(connectionString, filesystem);
        var list = new List<StoragePath>();
        var enumerator = containerClient.GetBlobsByHierarchyAsync().GetAsyncEnumerator();

        while (await enumerator.MoveNextAsync())
        {
            var current = enumerator.Current;
            list.Add(new StoragePath()
            {
                Name = current.Blob.Name,
                //IsDirectory = current.Blob.Properties.
            });
        }

        return list;
    }
public异步任务ListPathsAsync(字符串文件系统)
{
var containerClient=newblobcontainerclient(connectionString,filesystem);
var list=新列表();
var枚举器=containerClient.GetBlobsByHierarchyAsync().GetAsyncEnumerator();
while(等待枚举器.MoveNextAsync())
{
var current=枚举数。当前值;
添加(新的存储路径()
{
Name=current.Blob.Name,
//IsDirectory=current.Blob.Properties。
});
}
退货清单;
}
是否有其他属性需要检查?

如中所述:

Blob服务基于平面存储方案,而不是分层存储方案 计划。但是,您可以指定字符或字符串分隔符 在blob名称中创建虚拟层次结构

但是,使用
blobContainerClient.GetBlobs()
方法将直接返回具有完整路径的blob

但是,对于,它将只检索下一级目录和blob:

因此,您可以使用V11SDK,手动检查该项是目录还是blob

CloudStorageAccount cloudStorageAccount = CloudStorageAccount.Parse(connectionString);
CloudBlobClient cloudBlobClient = cloudStorageAccount.CreateCloudBlobClient();
CloudBlobContainer cloudBlobContainer = cloudBlobClient.GetContainerReference(containerName);

foreach(var item in cloudBlobContainer.ListBlobs())
{
    Console.WriteLine(item.Uri);
    if (item.GetType() == typeof(CloudBlobDirectory))
    {
        CloudBlobDirectory directory = (CloudBlobDirectory)item;
        Console.WriteLine("A directory, prefix is : " + directory.Prefix);
    }
}
如中所述:

Blob服务基于平面存储方案,而不是分层存储方案 计划。但是,您可以指定字符或字符串分隔符 在blob名称中创建虚拟层次结构

但是,使用
blobContainerClient.GetBlobs()
方法将直接返回具有完整路径的blob

但是,对于,它将只检索下一级目录和blob:

因此,您可以使用V11SDK,手动检查该项是目录还是blob

CloudStorageAccount cloudStorageAccount = CloudStorageAccount.Parse(connectionString);
CloudBlobClient cloudBlobClient = cloudStorageAccount.CreateCloudBlobClient();
CloudBlobContainer cloudBlobContainer = cloudBlobClient.GetContainerReference(containerName);

foreach(var item in cloudBlobContainer.ListBlobs())
{
    Console.WriteLine(item.Uri);
    if (item.GetType() == typeof(CloudBlobDirectory))
    {
        CloudBlobDirectory directory = (CloudBlobDirectory)item;
        Console.WriteLine("A directory, prefix is : " + directory.Prefix);
    }
}

几个月后,我使用
Microsoft.WindowsAzure.Storage,Version=9.3.1.0
返回到这个问题。
CloudBlobContainer.ListBlobsSegmentedAsync(..)
方法返回
IListBlobItem
的集合。您可以通过检查其具体类型来确定每个目录是否为目录

我有一个这样的模型课:

public async Task<List<StoragePath>> ListPathsAsync(string filesystem)
{
    string uri = $"https://{accountName}.{dnsSuffix}/{filesystem}?recursive=true&resource=filesystem";
    var result = await SendRequestAsync<StoragePathList>(HttpMethod.Get, uri, CancellationToken.None, true);
    return result.Content.Paths?.ToList();
}

public class StoragePath
{
    [JsonProperty(PropertyName = "isDirectory")]
    public bool IsDirectory { get; set; }

    // other properties
}
public class StoragePath
{
    public bool IsDirectory { get; set; }
    public string Name { get; set; }
}
我可以使用Automapper配置文件填充值,如下所示:

public class StorageProfile : Profile
{
    public StorageProfile()
    {
        CreateMap<IListBlobItem, StoragePath>()
            .ForMember(dest => dest.IsDirectory, prop => prop.MapFrom(src => src is CloudBlobDirectory))
            .ForMember(dest => dest.Name, prop => prop.MapFrom(src => GetName(src)));
    }

    private string GetName(IListBlobItem src)
    {
        switch (src)
        {
            case CloudBlobDirectory dir:
                return dir.Prefix;
            case CloudBlob blob:
                return blob.Name;
            default:
                throw new NotSupportedException();
        }
    }
}
公共类存储配置文件:配置文件
{
公共存储配置文件()
{
CreateMap()
.FormMember(dest=>dest.IsDirectory,prop=>prop.MapFrom(src=>src是CloudBlobDirectory))
.ForMember(dest=>dest.Name,prop=>prop.MapFrom(src=>GetName(src));
}
私有字符串GetName(IListBlobItem src)
{
开关(src)
{
案例CloudBlobDirectory目录:
返回目录前缀;
案例CloudBlob blob:
返回blob.Name;
违约:
抛出新的NotSupportedException();
}
}
}

几个月后,我使用Microsoft.WindowsAzure.Storage,Version=9.3.1.0返回到这个问题。
CloudBlobContainer.ListBlobsSegmentedAsync(..)
方法返回
IListBlobItem
的集合。您可以通过检查其具体类型来确定每个目录是否为目录

我有一个这样的模型课:

public async Task<List<StoragePath>> ListPathsAsync(string filesystem)
{
    string uri = $"https://{accountName}.{dnsSuffix}/{filesystem}?recursive=true&resource=filesystem";
    var result = await SendRequestAsync<StoragePathList>(HttpMethod.Get, uri, CancellationToken.None, true);
    return result.Content.Paths?.ToList();
}

public class StoragePath
{
    [JsonProperty(PropertyName = "isDirectory")]
    public bool IsDirectory { get; set; }

    // other properties
}
public class StoragePath
{
    public bool IsDirectory { get; set; }
    public string Name { get; set; }
}
我可以使用Automapper配置文件填充值,如下所示:

public class StorageProfile : Profile
{
    public StorageProfile()
    {
        CreateMap<IListBlobItem, StoragePath>()
            .ForMember(dest => dest.IsDirectory, prop => prop.MapFrom(src => src is CloudBlobDirectory))
            .ForMember(dest => dest.Name, prop => prop.MapFrom(src => GetName(src)));
    }

    private string GetName(IListBlobItem src)
    {
        switch (src)
        {
            case CloudBlobDirectory dir:
                return dir.Prefix;
            case CloudBlob blob:
                return blob.Name;
            default:
                throw new NotSupportedException();
        }
    }
}
公共类存储配置文件:配置文件
{
公共存储配置文件()
{
CreateMap()
.FormMember(dest=>dest.IsDirectory,prop=>prop.MapFrom(src=>src是CloudBlobDirectory))
.ForMember(dest=>dest.Name,prop=>prop.MapFrom(src=>GetName(src));
}
私有字符串GetName(IListBlobItem src)
{
开关(src)
{
案例CloudBlobDirectory目录:
返回目录前缀;
案例CloudBlob blob:
返回blob.Name;
违约:
抛出新的NotSupportedException();
}
}
}

谢谢。我希望能有一些比使用目录前缀更不黑客的东西,但如果这是API的一个限制,那就这样吧。谢谢你的帮助。我希望能有一些比使用目录前缀更不黑客的东西,但如果这是API的一个限制,那就这样吧。谢谢你的帮助。