如何检查CloudBlobDirectory是否存在? [英] How to check wether a CloudBlobDirectory exists or not?

查看:153
本文介绍了如何检查CloudBlobDirectory是否存在?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我正在编程的软件中,我试图在Azure的blob结构上创建虚拟文件系统.

In the software that I am programming, I am attempting to create a virtual file system over the blobs structure of Azure.

在此过程中,很多时候,我从系统获取一条路径,我需要告诉该路径是azure提供的Blob还是虚拟的BlobDirectory.我是通过将其从一种形式转换为另一种形式并处理错误来实现的.

Many times in the process, I get a path from the system and I need to tell whether the path is of a Blob or just a virtual BlobDirectory that azure provides. I did this by casting it from one form to another and handling the error.

但是现在,如果我知道路径指向虚拟目录,该如何检查该虚拟目录是否存在?

But now, if I know that a path points to a virtual directory, how can I check whether this virtual directory exists or not?

我可以使用以下代码获取对CloudBlobDirectory的引用:

I can get the reference to the CloudBlobDirectory with the following code:

var blobDirectory = client.GetBlobDirectoryReference("Path_to_dir");

推荐答案

在blob存储中,目录本身并不作为项目存在.您可以拥有的Blob的名称可以解释为位于目录中.如果您查看基础 REST API 您将看到目录没有任何内容.存储客户端库为您所做的工作是搜索以目录名称开头,然后以分隔符开头的blob,例如"DirectoryA/DirectoryB/FileName.txt".这意味着要存在一个目录,该目录必须包含一个blob.要检查目录是否存在,您可以尝试以下一种方法:

In blob storage, directories don't exist as an item by themselves. What you can have is a blob that has a name that can be interpreted as being in a directory. If you look at the underlying REST API you'll see that that there's nothing in there about directories. What the storage client library is doing for you is searching for blobs that start with the directory name then the delimiter e.g. "DirectoryA/DirectoryB/FileName.txt". What this means is that for a directory to exist it must contain a blob. To check if the directory exists you can try either:

var blobDirectory = client.GetBlobDirectoryReference("Path_to_dir");
bool directoryExists = blobDirectory.ListBlobs().Count() > 0

bool directoryExists = client.ListBlobsWithPrefix("DirectoryA/DirectoryB/").Count() > 0

我知道将目录中的所有内容都列出以获取计数并不是一个好主意,我相信您可以想出一种更好的方法.

I'm aware that listing everything in the directory just to get the count isn't that great an idea, I'm sure you can come up with a better method.

这篇关于如何检查CloudBlobDirectory是否存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆