How to delete a folder within an Azure blob container

前端 未结 8 1417
难免孤独
难免孤独 2020-12-29 19:18

I have a blob container in Azure called pictures that has various folders within it (see snapshot below):

I\'m trying to delete the folders tit

8条回答
  •  情书的邮戳
    2020-12-29 20:12

    Let's start with an example how to delete a "folder" using ListBlobsSegmentedAsyc:

    var container = // get container reference
    var ctoken = new BlobContinuationToken();
    do
    {
        var result = await container.ListBlobsSegmentedAsync("myfolder", true, BlobListingDetails.None, null, ctoken, null, null);
        ctoken = result.ContinuationToken;
        await Task.WhenAll(result.Results
            .Select(item => (item as CloudBlob)?.DeleteIfExistsAsync())
            .Where(task => task != null)
        );
    } while (ctoken != null);
    

    What it does...

    var ctoken = new BlobContinuationToken();
    

    A "folder" may contain a lot of files. ListBlobSegmentedAsyc may return only a part of them. This token will store the info where to continue in next call.

    var result = await container.ListBlobsSegmentedAsync("myfolder", true, BlobListingDetails.None, null, ctoken, null, null);
    
    • First argument is the required blob name ("path") prefix.
    • Second argument "useFlatBlobListing=true" tells the client to return all items in all sub folders. If set to false, it will run in "virtual folders" mode and behave like a file system.
    • The ctoken will tell azure where to continue

    For all arguments see https://docs.microsoft.com/en-us/dotnet/api/microsoft.windowsazure.storage.blob.cloudblobclient.listblobssegmentedasync?view=azure-dotnet for details.

    (item as CloudBlob)?.DeleteIfExistsAsync()
    

    Now we have a list of IListBlobItem in result.Results. Because an IListBlobItem is not guaranteed to be a deletable CloudBlob (e.g. it could be a virtual folder if we would have set useFlatBlobListing=false), we try to cast it and delete it if possible.

    result.Results.Select(item => (item as CloudBlob)?.DeleteIfExistsAsync())
    

    Triggers delete for all results and returns a list of tasks.

    .Where(task => task != null)
    

    If Results contained items we could not cast to CloudBlob, our list of tasks contains null values. We have to remove them.

    ... then we wait until all delete for current segment finished and continue with next segment if available.

提交回复
热议问题