本文介绍了C#从Azure检索Blob列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要一些存档清理代码,以便在特定保留期限后删除旧的Azure日志.
I need to have some archive cleanup code to remove old Azure logs after a certain retention period has occurred.
我知道我可以这样做:
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(""); CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); CloudBlobContainer container = blobClient.GetContainerReference("ctr"); var blobList = container.ListBlobs(); foreach(var blob in blobList) { logger.Info($"Blob Name: {blob.Uri}"); }但是在我的容器中,结构是
However within my container the structure is
/ /year/month/day/hour/files所以现在有
/2017/5/11/14/files /2017/5/11/17/files /2017/5/11/22/files /2017/5/11/23/files和
/2017/5/12/11/files其中文件是多个备份文件.
Where files is multiple backup files.
因为2017文件夹是根目录,所以for循环的集合中只有1个项目.
The for loop only has 1 item in it's collection as the 2017 folder is the root.
有没有办法检索所有斑点?
Is there a way to retrieve all blobs?
最终目标是删除所有早于保留期的斑点.
The end goal is to delete all blobs older than the retention period.
推荐答案尝试此模式.在浏览大型存储时可以方便使用.我发现它对GC和内存占用空间更为友好
Try this pattern. Can be handy when browsing big storages. I found it much more GC and memory footprint friendly
var blobAccount = "<account>"; var apiKey = "<api-key>"; var containerName = "<container>"; var storageCredentials = new StorageCredentials(blobAccount, apiKey); var account = new CloudStorageAccount(storageCredentials, true); var blobClient = account.CreateCloudBlobClient(); var container = blobClient.GetContainerReference(containerName); var blobLimit = 500 if (container == null) { return; } var blobContinuationToken = new BlobContinuationToken(); using (var fs = new FileStream("Output.csv", FileMode.Create)) { var sw = new StreamWriter(fs); sw.WriteLine("Type,Name,Length"); BlobContinuationToken continuationToken = null; do { var blobList = container.ListBlobsSegmented("", true, BlobListingDetails.Metadata, blobLimit, continuationToken, new BlobRequestOptions { LocationMode = LocationMode.PrimaryOnly }, null); continuationToken = blobList.ContinuationToken; // I was looking only for BlockBlobs foreach (var item in blobList.Results.OfType<CloudBlockBlob>()) { sw.WriteLine($"block,\"{item.Name}\",{item.Properties.Length}"); } } while (continuationToken != null); }更多推荐
C#从Azure检索Blob列表
发布评论