C# retrieving a list of blobs from Azure

后端 未结 2 2081
你的背包
你的背包 2021-01-07 05:54

I need to have some archive cleanup code to remove old Azure logs after a certain retention period has occurred.

I am aware that I can do this:

Cloud         


        
2条回答
  •  甜味超标
    2021-01-07 06:28

    Use the UseFlatBlobListing parameter like this:

    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}");
    }
    

    This will give you all blobs in a flattened way.

    See https://docs.microsoft.com/en-us/dotnet/api/microsoft.windowsazure.storage.blob.cloudblobcontainer.listblobs?view=azure-dotnet

    If you also include the prefix parameter you can filter results based on the folder structure. To get everything in may 2017 you can do

    var blobList = container.ListBlobs(prefix: "2017/5/", useFlatBlobListing: true)
    

    This might help reducing the list of blobs depending on your retention.

提交回复
热议问题