簡體   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