我在蔚蓝中创建了blob存储库。
然后创建了名为"MyReport“的容器
在容器"MyReport“中,我创建了两个名为"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创建的最新文件夹。
怎么做?
发布于 2019-10-03 02:10:31
更新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());结果(文件夹名的末尾有一个"/“,您可以根据需要删除它):

根据这个issue,当列表blob时,blob是通过比较blob的名称char- by char(升序)来排序的。
因此,在您的代码中,只需使用ListBlobs方法,然后使用.Last()获取最新的方法。
样本代码:
#other code
var myblob = container.ListBlobs().Last();
Console.WriteLine(((CloudBlockBlob)myblob).Name);结果:

发布于 2019-10-03 09:58:34
实际上,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();发布于 2019-10-04 09:29:53
我从“伊万·杨”那里得到了一些线索,我找到了答案。
线索是使用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;https://stackoverflow.com/questions/58201252
复制相似问题