Home > OS >  Azure Blob Storage Returns 404 from Azure App Service
Azure Blob Storage Returns 404 from Azure App Service

Time:04-12

I have .NET Core 5 API that will call Azure Blob Storage to retrieve a file and return it back to the client in the form of base 64 string.

The code works perfectly in my local machine but when I deploy the code into Azure App Service, it always return 404 without any other error message.

This is the code that I use to download the file from Azure Blob Storage:

    public async Task<string> GetDocumentBase64StringAsync(
        string filePath,
        ContainerCategory containerCategory,
        CancellationToken cancellationToken)
    {
        var cleanedFilePath = filePath.Replace(@"\\", @"\");
        var containerName = GetContainerName(containerCategory);
        var containerClient = _blobServiceClient.GetBlobContainerClient(containerName);
        var blobClient = containerClient.GetBlobClient(cleanedFilePath);

        using var memoryStream = new MemoryStream();
        await blobClient.DownloadToAsync(memoryStream, cancellationToken);
        var bytes = memoryStream.ToArray();
        var base64String = Convert.ToBase64String(bytes);
        return $"data:image/png;base64,{base64String}";
    }

The blob client itself is using this constructor:

var blobServiceClient = new BlobServiceClient(azureBlobStorageSettings.ConnectionString);

The connection string is using the Connection String from Azure Portal > Storage Account > Access Key

Is there any specific settings that I need to enable on both Blob Storage and App Service in Azure Portal to make this work?

UPDATE:

The filePath value will look like this: ScanDocs\\BOM\\BOM06161\\ba68f94ddd.pdf

If I hard code the value inside the code, it works:

var blobClient = containerClient.GetBlobClient("ScanDocs\\BOM\\BOM06161\\ba68f94ddd.pdf");

but if it's coming from the parameter, it does not work.

CodePudding user response:

The reason is because locally you have full access to local storage, while running on App Service, as it's a shared environment, you need to use Temp folder. The environment variable %TEMP% will return D:\local which should be used to upload / read the files you need.

As another option, you can upload the files to a storage account and the files from it.

more info: https://github.com/projectkudu/kudu/wiki/Understanding-the-Azure-App-Service-file-system

CodePudding user response:

I change the API method from GET to POST and pass in the filepath in the request body and it works fine now.

Previously, the API signature is:

[HttpGet]
[Route("document/{filepath}")]
public async Task<ActionResult<string>> GetDocumentBase64StringAsync(
   string filepath,
   CancellationToken cancellationToken = default)

and now:

[HttpPost]
[Route("document")]
public async Task<ActionResult<string>> GetDocumentBase64StringAsync(
   [FromBody] GetDocumentBase64Request request,
   CancellationToken cancellationToken = default)

So I guess the problem is when passing the filepath as URL parameter, the \\ is messed up because of the encoding.

If there is other method that could handle the \\ in the URL parameter, please let me know.

  • Related