skip to Main Content

I have a method below to read blob file as per the fileName. The file exists in the Azure Blob Storage but still the code is throwing "The specified blob does not exists."

public async Task<string> ReadBlob(string fileName)
{
    try
    {
        CloudBlobClient cloudBlobClient = CloudStorageAccount.Parse(_blobDataConnectionString)
                                                                .CreateCloudBlobClient();

        var cc = cloudBlobClient.GetContainerReference(_blobDataContainerName);
        CloudBlockBlob blockBlob = cc.GetBlockBlobReference(fileName);

        return await blockBlob.DownloadTextAsync();
    }
    catch (Exception ex)
    {
        _logger.LogError(ex, $"ReadBlob: {ex.Message}");
        throw;
    }
}

Below is the file I am trying to Read.
BlobContainer

2

Answers


  1. Chosen as BEST ANSWER

    I have modified my Read method and now I am able to read the data from Blob

    public async Task<string> ReadBlob(string fileName)
    {
    try
    {
        var blobContainerClient = await _blobStorageClientFactory.CreateClient(_blobDataConnectionString, _blobDataContainerName);
       
        var blobClient = blobContainerClient.GetBlobClient(fileName);
        var response =  blobClient.DownloadStreamingAsync();
    
        using StreamReader reader = new(response.Result.Value.Content);
        return await reader.ReadToEndAsync();
    }
    catch (Exception ex)
    {
        _logger.LogError(ex, $"ReadBlob: {ex.Message}");
        throw;
    }}
    

  2. I too agree with @David Makogon that it is a basic error, but my code works, when blob and container exists, it gives blob data . If not throws an exception saying either of one does not exist and below are my observations:

    using Microsoft.WindowsAzure.Storage;
    using Microsoft.WindowsAzure.Storage.Blob;
    
    var rith_cs = @"DefaultEndpointsProtocol=https;AccountName=rith54;AccountKey=vejgTwTStwCE1RQ==;EndpointSuffix=core.windows.net";
    var rith_containerName="rithwik";
    var rith_bn = "test11.txt";
    CloudStorageAccount rith_sa = CloudStorageAccount.Parse(rith_cs);
    CloudBlobClient rith_bc = rith_sa.CreateCloudBlobClient();
    CloudBlobContainer rith_cn = rith_bc.GetContainerReference(rith_containerName);
    if (!await rith_cn.ExistsAsync())
    {
        throw new ApplicationException($"Hello Rithwik, Container {rith_containerName} does not exist.");
    }
    CloudBlockBlob blockBlob = rith_cn.GetBlockBlobReference(rith_bn);
    if (!await blockBlob.ExistsAsync())
    {
        throw new ApplicationException($"Hello Rithwik, Blob {rith_bn} does not exist in container {rith_containerName}.");
    }
    var content = await blockBlob.DownloadTextAsync();
    Console.WriteLine(content);
    

    My blob exists:

    enter image description here

    After Running the code:

    enter image description here

    Make sure you give all correct values(filename, container name and connection string). Make sure that the container is accessible. Filename should be given with extension too.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search