Search code examples
azure-functionsazure-blob-storagequery-stringazure-sas

Blob SAS URI character decoded when passed as Query parameter to Azure Function app


I wanted to pass the SAS URI of a image file to Azure Functions as query parameters. But when I pass the below URL in GET call, some characters from 'sig' part get decoded https://ABCD.blob.core.windows.net/images/test.jpg?sp=r&st=2021-05-06T11:30:21Z&se=2022-05-06T19:30:21Z&spr=https&sv=2020-02-10&sr=b&sig=JuPyAR%2F5WNeSVXj4G%2Fft9QDMzL%2BtXSywSS375jZpjXQ%3D

The above URL becomes https://ABCD.blob.core.windows.net/images/test.jpg?sp=r&st=2021-05-06T11:30:21Z&se=2022-05-06T19:30:21Z&spr=https&sv=2020-02-10&sr=b&sig=JuPyAR/5WNeSVXj4G/ft9QDMzL+tXSywSS375jZpjXQ=

%2F to / %2B to + %3D to =

Because of this when I try to access the blob, I get error:

System.Private.CoreLib: Exception while executing function: FUNCTIONNAME. System.Net.Requests: The remote server returned an error: (403) Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature..

I am accessing the URI using below code

string image = req.Query["image"];


Solution

  • Before you send the image URL to Azure function as a request param, pls use base64 to encode the whole URL and in your Azure function decode it just as below:

    string image = req.Query["image"];
    
    string base64Encoded = image;
    string base64Decoded;
    byte[] data = System.Convert.FromBase64String(base64Encoded);
    base64Decoded = System.Text.ASCIIEncoding.ASCII.GetString(data);
    log.LogInformation(image);
    log.LogInformation(base64Decoded);
    

    so that you can get the original URL. I have tested on my side, it works for me perfectly:

    enter image description here

    Let me know if you have any more questions.