I'm not seeing any examples online on how to get all the blobs located inside a certain directory within a BlobContainerClient
.
Previously, I was using the Microsoft.Azure.Storage
packages, but those have since been deprecated. My old code that was scanning a directory for all blobs was:
public async Task<void> ListAllBlobs(string path)
{
var myContainer = await GetCloudBlobClientAsync();
var directory = myContainer.GetDirectoryReference(path);
var blobs = await directory.ListBlobsSegmentedAsync(true, BlobListingDetails.None,
blobSettings.MaxResult, null, null, null);
var results = blobs.Results;
foreach(CloudBlockBlob b in results)
{
// non-relevant code
}
}
private async Task<CloudBlobContainer> GetCloudBlobClientAsync()
{
var storageAccount = CloudStorageAccount.Parse(azureBlobStorageConnectionString);
var blobClient = storageAccount.CreateCloudBlobClient();
var container = blobClient.GetContainerReference(blobStorageSettings.ContainerName);
if (!await container.ExistsAsync())
{
await container.CreateAsync();
}
return container;
}
Essentially, I'm moving the above code from Microsoft.Azure.Storage
over to Azure.Storage.Blobs
.
If I were to recreate the ListAllBlobs(string path)
function to use Azure.Storage.Blobs
, I'm confused on how to setup a container and then access an inner container based on a path that's passed in - then cycle through the blobs that exist within that container. Can anyone help?
Here's what I have so far:
public async Task<void> ListAllBlobs(string path)
{
var myContainer = await GetCloudBlobClientAsync();
var directory = myContainer.GetBlobClient(path);
// This doesn't work because I can't do 'GetBlobs' on the Client, only on the container.
foreach(BlobItem blob in directory.GetBlobs(Blobtraits.None, BlobStates.None, string.Empty))
{
// more non-relevant code
}
}
To clarify, in the above code, it doesn't like that I'm calling GetBlobs
on a Client, rather than on the Container, but I can't pass in a path to the container.
GetBlobsAsync(prefix=path)
, the error is "The name 'prefix' does not exist in the current context" – Flick