1

I am trying to create a function for all blobs in a container. I took the help How to get hold of all the blobs in a Blob container which has sub directories levels(n levels)?, which seems to use an overload that doesn't exist any more. I had added default values into the additional fields prefix and operationContext :

static internal async Task<List<string>> ListBlobNames(string ContainerName)
{

    BlobContinuationToken continuationToken = null;
    bool useFlatBlobListing = true;
    BlobListingDetails blobListingDetails = BlobListingDetails.None;
    var blobOptions = new BlobRequestOptions();
    CloudBlobContainer container = Container(ContainerName);
    var operationContext = new OperationContext();
    var verify = container.GetBlobReference("A_Valid_Name.jpg");
    var verify2 = container.GetBlobReference("NotAName.jpg");

    using (var a = await verify.OpenReadAsync()) ;
    //using (var a = await verify2.OpenReadAsync());   // doesn't work since it doesn't exist

    return (await container.ListBlobsSegmentedAsync("", useFlatBlobListing, blobListingDetails, null, continuationToken, blobOptions, operationContext))
            .Results.Select(s => s.Uri.LocalPath.ToString()).ToList();
}

The last line gave me an exception:

StorageException: The requested URI does not represent any resource on the server.

I then created the verfiy and verify2 variables to test if my container is valid. verify references a valid blob and verify2 references an invalid blob name. Running the code with the second using statement uncommented gave me an error in the second using statement. This shows that the verify works and thus the container is valid.

Neville Nazerane
  • 6,622
  • 3
  • 46
  • 79

2 Answers2

4

I am trying to create a function for all blobs in a container.

You could leverage the Azure Storage Client Library and install the package WindowsAzure.Storage, then you could follow the tutorial List blobs in pages asynchronously to achieve your purpose. For test, I just created my .Net Core console application as follows:

static void Main(string[] args)
{
    MainAsync(args).GetAwaiter().GetResult();
}

static async Task MainAsync(string[] args)
{   
    // Retrieve storage account from connection string.
    CloudStorageAccount storageAccount = CloudStorageAccount.Parse("{your-storage-connection-string}");

    // Create the blob client.
    CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

    string containerName = "images";
    // Retrieve reference to a previously created container.
    CloudBlobContainer container = blobClient.GetContainerReference(containerName);

    if (await container.ExistsAsync())
        await ListBlobsSegmentedInFlatListing(container);
    else
        Console.WriteLine($"Your container with the name:{containerName} does not exist!!!");

    Console.WriteLine("press any key to exit...");
    Console.ReadLine();
}

ListBlobsSegmentedInFlatListing:

async public static Task ListBlobsSegmentedInFlatListing(CloudBlobContainer container)
{
    //List blobs to the console window, with paging.
    Console.WriteLine("List blobs in pages:");

    int i = 0;
    BlobContinuationToken continuationToken = null;
    BlobResultSegment resultSegment = null;

    //Call ListBlobsSegmentedAsync and enumerate the result segment returned, while the continuation token is non-null.
    //When the continuation token is null, the last page has been returned and execution can exit the loop.
    do
    {
        //This overload allows control of the page size. You can return all remaining results by passing null for the maxResults parameter,
        //or by calling a different overload.
        resultSegment = await container.ListBlobsSegmentedAsync("", true, BlobListingDetails.All, 10, continuationToken, null, null);
        if (resultSegment.Results.Count<IListBlobItem>() > 0) { Console.WriteLine("Page {0}:", ++i); }
        foreach (var blobItem in resultSegment.Results)
        {
            Console.WriteLine("\t{0}", blobItem.StorageUri.PrimaryUri);
        }
        Console.WriteLine();

        //Get the continuation token.
        continuationToken = resultSegment.ContinuationToken;
    }
    while (continuationToken != null);
}

Test:

enter image description here

Bruce Chen
  • 18,207
  • 2
  • 21
  • 35
-1

This works for me...

String myContainerName = "images";

CloudBlobContainer blobContainer = blobClient.GetContainerReference(myContainerName);

CloudBlockBlob blockBlob; 

blockBlob = blobContainer.GetBlockBlobReference("NotAName.jpg");

bool verify2 = await blockBlob.ExistsAsync();
if (!verify2)
{
 // the blob image does not exist
 // do something 
}
Newport99
  • 483
  • 4
  • 21
  • 1
    the verify part was a test to make sure the container is working. that is not where the issue was. I was trying to list the blobs – Neville Nazerane Jan 01 '18 at 00:16