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

时间:2019-10-02 12:10:47

标签: c# azure azure-blob-storage

我已经在Azure中创建了Blob存储。

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

在内部容器“ MyReport”中,我创建了2个名为“测试”和“实时”的文件夹。在“测试”和“实时”这两个文件夹下,都有许多子文件夹。

我想要获得的是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创建的最新文件夹。

该怎么做?

3 个答案:

答案 0 :(得分:1)

更新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());

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

enter image description here


根据此issue,在列出blob时,通过比较blob的名称char-by-char(升序)来对blob进行排序。

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

示例代码:

#other code

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

结果:

enter image description here

答案 1 :(得分:0)

实际上,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();

答案 2 :(得分:0)

通过@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;