我需要一些归档清理代码,以便在发生特定保留期后删除旧的Azure日志。
我知道我可以这样做:
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}");
}
然而,在我的容器中,结构是
/
/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
文件是多个备份文件。
for循环在其集合中只有1个项目,因为2017文件夹是根目录。
有没有办法检索所有blob?
最终目标是删除所有早于保留期的blob。
答案 0 :(得分:4)
尝试这种模式。浏览大型存储时可以很方便。我发现它更多的GC和内存足迹友好
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);
}
答案 1 :(得分:3)
使用UseFlatBlobListing
参数,如下所示:
CloudStorageAccount storageAccount = CloudStorageAccount.Parse("");
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference("ctr");
var blobList = container.ListBlobs(useFlatBlobListing: true)
foreach(var blob in blobList)
{
logger.Info($"Blob Name: {blob.Uri}");
}
这将以平坦的方式给你所有的blob。
如果您还包含prefix
参数,则可以根据文件夹结构过滤结果。为了获得2017年5月的所有内容,你可以做到
var blobList = container.ListBlobs(prefix: "2017/5/", useFlatBlobListing: true)
这可能有助于减少blob列表,具体取决于您的保留期。