Home > Net >  Getting error "404 The specified blob does not exist" when downlading blob using Uri
Getting error "404 The specified blob does not exist" when downlading blob using Uri

Time:08-13

I am trying to download blob using blob url in Service Bus Topic trigger Azure function, but getting below error:

Status: 404 (The specified blob does not exist.)
 ErrorCode: BlobNotFound
Content:
<?xml version="1.0" encoding="utf-8"?><Error><Code>BlobNotFound</Code><Message>The specified blob does not exist.
</Message></Error>

Below is my Startup.cs file:

[assembly: FunctionsStartup(typeof(Startup))]
namespace Emails.Dispatcher
{
    public class Startup : FunctionsStartup
    {
        public override void Configure(IFunctionsHostBuilder builder)
        {
            builder.Services.AddSingleton(x =>
            new BlobServiceClient(connectionString: Environment.GetEnvironmentVariable("AzureWebJobsStorage")));

            builder.Services.AddSingleton<IBlobService, BlobService>();

            builder.Services.AddLogging();
            builder.Services.AddHttpClient();
            builder.AddHelpers();
            builder.Services.AddWorkers();

        }
    }
}

BlobService:

public class BlobService : IBlobService
{
    private readonly BlobServiceClient _blobServiceClient;

    public BlobService(BlobServiceClient blobServiceClient)
    {
        this._blobServiceClient = blobServiceClient;
    }

    public async Task<byte[]> DownloadAsync(string containerName, string fileUri)
    {
        var containerClient = _blobServiceClient.GetBlobContainerClient(containerName);

        var blobClient = containerClient.GetBlobClient(fileUri);

        using (var ms = new MemoryStream())
        {
            await blobClient.DownloadStreamingAsync();
            return ms.ToArray();
        }
    }

    public async Task<BlobDto> UploadAsync(string containerName, string filename, string content)
    {
        var containerClient = _blobServiceClient.GetBlobContainerClient(containerName);

        var blobClient = containerClient.GetBlobClient(filename);

        var bytes = Encoding.UTF8.GetBytes(content);

        await using var memoryStream = new MemoryStream(bytes);

        await blobClient.UploadAsync(content);

        return new BlobDto
        {
            Uri = blobClient.Uri,
            Name = blobClient.Name
        };
    }
}

public interface IBlobService
{
    Task<BlobDto> UploadAsync(string containerName, string filename, string content);

    Task<byte[]> DownloadAsync(string containerName, string fileUri);
}

Actually this service is receiving full blob url (i.e. https://{storage-url}.blob.core.windows.net/{container-name}/files/unzip/691ec307-7c2b-4aaa-8f75-8742c9faa9f5/1615015726/{file-name}.pdf).

Am i missing anythigng here?

CodePudding user response:

The issue is with the following line of code:

var blobClient = containerClient.GetBlobClient(fileUri);

When you create a BlobClient using ContainerClient and GetBlobClient, you need to provide just the name of the blob and not the entire URL.

To fix the issue, just pass the blob name.

CodePudding user response:

I am able to download blob file with the help of Uri and and Name property of CloudBlockBlob. As shown below:

  public async Task<byte[]> DownloadAsync(string containerName, string fileUri)
        {
            var containerClient = _blobServiceClient.GetBlobContainerClient(containerName);

            var blobUri = new System.Uri(fileUri);

            var name = new CloudBlockBlob(blobUri).Name;

            var blobClient = containerClient.GetBlobClient(name);

            var response = await blobClient.DownloadStreamingAsync();

            using MemoryStream ms = new MemoryStream();

            await response.Value.Content.CopyToAsync(ms);

            ms.Position = 0;

            return ms.ToArray();
        }
  • Related