Signature did not match error only occurs when SAS token is generated by deployed code

Robson, James 71 Reputation points
2022-08-05T15:11:40.99+00:00

I have the code below to generate a URL that can be used to download a blob. When I run the code locally and trigger it with an HTTP request, it returns a URL that works. I can use it to download the blob.

However, when I deploy the code to Azure, it returns a URL that looks similar to the one generated locally, but when I try to use that URL, I get the error mentioned in the title.

Here's the code:

def main(req: func.HttpRequest) -> func.HttpResponse:  
    region = req.params.get('region')  
    category = req.params.get('category')  
  
    if region and category:  
        blob_name = f'{category}_{region}.csv'  
        sas_token = generate_account_sas(  
            account_name = os.environ['STORAGE_ACCOUNT'],  
            account_key = os.environ['STORAGE_ACCOUNT_KEY'],  
            resource_types = ResourceTypes(object = True),   
            permission = AccountSasPermissions(read = True),  
            expiry = datetime.now() + timedelta(hours = int(os.environ['DOWNLOAD_TIME_LIMIT']))   
        )  
        return func.HttpResponse(f'{os.environ["STORAGE_CONTAINER_URI"]}/{blob_name}?{sas_token}')  
    else:  
        logging.warning('GenerateSignedUrl received a bad request')  
        return func.HttpResponse(  
             "Required parameters not found in the request.",  
             status_code=400  
        )  

When I deploy the code to Azure and trigger it with an HTTP request, it works and returns a URL. However, when I try to use that URL to download the blob, I get the error below:

<Error>
<Code>AuthenticationFailed</Code>
<Message>Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.
RequestId:c35ecfe2-801e-0015-38da-a8cc16000000
Time:2022-08-05T14:48:42.3053987Z</Message>
<AuthenticationErrorDetail>Signature did not match. String to sign used was stp20220331
r
b
o

2022-08-07T14:33:10Z

2021-06-08

</AuthenticationErrorDetail>
</Error>

Azure Blob Storage
Azure Blob Storage
An Azure service that stores unstructured data in the cloud as blobs.
{count} votes

Answer accepted by question author
  1. shiva patpi 13,376 Reputation points Microsoft Employee Moderator
    2022-08-05T23:33:50.97+00:00

    Hello @Robson, James - I am glad that you are able to identity the issue from your end. Yeah - that particular ERROR CODE always points to either Incorrect
    Storage Account Key
    (or)
    SAS Key
    (or)
    Sometimes, there might be some special characters in the SAS Key.

    1 person found this answer helpful.
    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Robson, James 71 Reputation points
    2022-08-05T15:45:40.39+00:00

    It turns out there was an error in the config settings. Specifically, the STORAGE_ACCOUNT_KEY had an incorrect value.

    0 comments No comments

  2. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.