繁体   English   中英

从 Azure Blob 存储容器获取最新文件夹

[英]Getting latest folder from Azure Blob Storage container

我在 azure 中创建了 blob 存储。

然后创建了名为“MyReport”的容器

在容器“MyReport”中,我创建了 2 个文件夹,分别称为“Test”和“Live”。 在“Test”和“Live”两个文件夹下都有许多子文件夹。

我想要的是在这些文件夹中获取由 azure 创建的最新文件夹。

我尝试了以下方法:

StorageCredentialsAccountAndKey credentials = new  StorageCredentialsAccountAndKey(accountName, accessKey);
CloudStorageAccount acc = new CloudStorageAccount(credentials, true);
CloudBlobClient client = acc.CreateCloudBlobClient();
CloudBlobDirectory container = client.GetBlobDirectoryReference(@"MyReport/Test");

var folders = container.ListBlobs().Where(b => b as CloudBlobDirectory != null).ToList();

在文件夹变量中,我得到了许多文件夹,但我想获取由 azure 创建的最新文件夹。

这个怎么做?

10/04 更新:

            CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient();
            CloudBlobContainer cloudBlobContainer = cloudBlobClient.GetContainerReference("test1");
            CloudBlobDirectory myDirectory = cloudBlobContainer.GetDirectoryReference("test");
            var myfiles = myDirectory.ListBlobs(useFlatBlobListing: true, blobListingDetails: BlobListingDetails.All).Where(b => b as CloudBlockBlob != null);

            var my_lastmodified_blob = myfiles.OfType<CloudBlockBlob>().OrderByDescending(b => b.Properties.LastModified).First();
            Console.WriteLine(my_lastmodified_blob.Parent.StorageUri.PrimaryUri.Segments.Last());

结果(文件夹名称末尾有一个“/”,您可以根据需要将其删除):

在此处输入图像描述


根据这个问题,当列出 blob 时,通过逐个字符(升序)比较 blob 的名称来对 blob 进行排序。

因此,在您的代码中,只需使用ListBlobs方法,然后使用.Last()来获取最新的。

示例代码:

#other code

var myblob = container.ListBlobs().Last();
Console.WriteLine(((CloudBlockBlob)myblob).Name);

结果:

在此处输入图像描述

实际上 CloudBlobDirectory 不保存 LastModified 日期,但在文件夹中所有 CloudBlockBlob 都保存上次修改日期。 所以我们应该根据内部文件来决定

这是示例及其对我有用

CloudBlobClient client = acc.CreateCloudBlobClient();
var container = client.GetContainerReference(@"seleniumtestreports");
CloudBlobDirectory Directory = container.GetDirectoryReference("DevTests");
var BlobFolders = Directory.ListBlobs().OfType<CloudBlobDirectory>()        .Select(f => new { cloudBlobDirectory = f,LastModified = f.ListBlobs().OfType<CloudBlockBlob>().OrderByDescending(dd => dd.Properties.LastModified).FirstOrDefault().Properties.LastModified }).ToList();

var getLastestFolder = BlobFolders.OrderByDescending(s => s.LastModified).FirstOrDefault();

借助@Ivan Yang 的一些线索,我找到了答案。

线索是使用 BlobRequest 选项。 所以这对我有用

StorageCredentialsAccountAndKey credentials = new StorageCredentialsAccountAndKey(accountName, accessKey);

        CloudStorageAccount acc = new CloudStorageAccount(credentials, true);

        CloudBlobClient client = acc.CreateCloudBlobClient();
        CloudBlobDirectory container = client.GetBlobDirectoryReference(@"MyReport/Test");

        BlobRequestOptions options = new BlobRequestOptions();
        options.UseFlatBlobListing = true;
        var listblob = container.ListBlobs(options);

        var latestFolderAzure = listblob.OfType<CloudBlob>().OrderBy(b => b.Properties.LastModifiedUtc).LastOrDefault()?.Parent.Uri.AbsoluteUri;

暂无
暂无

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

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