简体   繁体   English

在给定url文件夹路径的情况下从azure存储获取文件名

[英]Getting filenames from azure storage given url folder path

For starters, yes I know there are no "real" folders in Azure storage, but given a url path to an image of 对于初学者,是的,我知道Azure存储中没有“真实”文件夹,但给出了图像的URL路径

https://myazurestorage.blob.core.windows.net/accessibleimages/folder/folder/myimage.JPG https://myazurestorage.blob.core.windows.net/accessibleimages/folder/folder/myimage.JPG

Is there a method to generate the list of filenames by passing in the base url of 有没有一种方法可以通过传入的基本URL来生成文件名列表

https://myazurestorage.blob.core.windows.net/accessibleimages/folder/folder/ https://myazurestorage.blob.core.windows.net/accessibleimages/folder/folder/

I can't seem to find any reference to one. 我似乎无法找到任何一个参考。 I can get a list of blobs, but they are the base "folders" in the containers but I need to drill deeper to get the specific filenames. 我可以获得blob列表,但它们是容器中的基本“文件夹”,但我需要深入钻取以获取特定的文件名。

Here's how you can do this: 这是你如何做到这一点:
1. Create blob client: 1.创建blob客户端:

var account = new CloudStorageAccount(new StorageCredentials("myazurestorage", YOUR_API_KEY), true);
var blobClient = account.CreateCloudBlobClient();  

2. Get container: 2.获取容器:

var container = blobClient.GetContainerReference("acessibleimages");

3. Use ListBlobs like this (read more about flat blob listing ): 3.使用像这样的ListBlobs(了解有关blob blob列表的更多信息):

var blobList= container.ListBlobs(prefix: "folder/folder", useFlatBlobListing: true);
  1. After you have your list of blobs you can iterate through them and extract path like this: foreach (var blob in blobs){blob.StorageUri.PrimaryUri;} 获得blob列表后,可以迭代它们并提取如下所示的路径: foreach (var blob in blobs){blob.StorageUri.PrimaryUri;}

RAS's answer is correct but I have a solution with only two steps. RAS的答案是正确的,但我只有两个步骤。 Since, according to the official document , there is also a ListBlobs function in the CloudBlobClient class, we can get the blobs without creating the container object. 因为根据官方文档 ,CloudBlobClient类中还有一个ListBlobs函数,我们可以在不创建容器对象的情况下获取blob。 the following are the steps: 以下是步骤:

  1. Create blob client: 创建blob客户端:
var account = new CloudStorageAccount(new StorageCredentials("myazurestorage", YOUR_API_KEY), true);
var blobClient = account.CreateCloudBlobClient();
  1. Use ListBlobs like this: 像这样使用ListBlobs:
var blobList= container.ListBlobs(prefix: "folder/folder/", useFlatBlobListing: true);

Be noticed of the ending forward slash(/) . 注意结束正斜杠(/) if not added, the function returns the folder per se. 如果没有添加,该函数返回文件夹本身。 if added, returns the blobs in the folder. 如果添加,则返回文件夹中的blob。

And unlike web development, if the prefix starts with "/", it will not work and return 404 (cannot find page.) 与Web开发不同,如果前缀以“/”开头,则它将无效并返回404(无法找到页面。)

Also Azure Storage is case sensitive. Azure存储区分大小写。

Use useFlatBlobListing , according to what you want. 根据你的需要使用useFlatBlobListing Why microsoft just use the word recursive? 为什么微软只使用递归这个词? I think recursive is better. 我认为递归更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM