Migrating from Microsoft.Azure.Storage.Blob to Azure.Storage.Blobs - directory concepts missing
Asked Answered
H

1

7

These are great guides for migrating between the different versions of NuGet package:

https://github.com/Azure/azure-sdk-for-net/blob/Azure.Storage.Blobs_12.6.0/sdk/storage/Azure.Storage.Blobs/README.md

https://elcamino.cloud/articles/2020-03-30-azure-storage-blobs-net-sdk-v12-upgrade-guide-and-tips.html

However I am struggling to migrate the following concepts in my code:

// Return if a directory exists:
container.GetDirectoryReference(path).ListBlobs().Any();

where GetDirectoryReference is not understood and there appears to be no direct translation.

Also, the concept of a CloudBlobDirectory does not appear to have made it into Azure.Storage.Blobs e.g.

private static long GetDirectorySize(CloudBlobDirectory directoryBlob) {
    long size = 0;

    foreach (var blobItem in directoryBlob.ListBlobs()) {
        if (blobItem is BlobClient)
            size += ((BlobClient) blobItem).GetProperties().Value.ContentLength;

        if (blobItem is CloudBlobDirectory)
            size += GetDirectorySize((CloudBlobDirectory) blobItem);
    }

    return size;
}

where CloudBlobDirectory does not appear anywhere in the API.

Hammers answered 22/10, 2020 at 10:28 Comment(0)
K
3

There's no such thing as physical directories or folders in Azure Blob Storage. The directories you sometimes see are part of the blob (e.g. folder1/folder2/file1.txt). The List Blobs requests allows you to add a prefix and delimiter in a call, which are used by the Azure Portal and Azure Data Explorer to create a visualization of folders. As example prefix folder1/ and delimiter / would allow you to see the content as if folder1 was opened.

That's exactly what happens in your code. The GetDirectoryReference() adds a prefix. The ListBlobs() fires a request and Any() checks if any items return.

For V12 the command that'll allow you to do the same would be GetBlobsByHierarchy and its async version. In your particular case where you only want to know if any blobs exist in the directory a GetBlobs with prefix would also suffice.

Keynote answered 22/10, 2020 at 13:30 Comment(2)
But CloudBlobDirectory was part of the old api. Is your answer there is no equivalent in the new api?Hammers
My answer is that GetBlobsByHierarchy is the equivalent although it requires you to define a few more parameters yourself, which I explained in the post.Keynote

© 2022 - 2024 — McMap. All rights reserved.