getting authentication error when blob name has space

Rishabha Dwivedi 0 Reputation points
2023-06-23T04:11:18.2433333+00:00
def get_blob_url(file_name,account_name,container_name,account_key):    # handling space in file name    file_name = file_name.replace(' ','%20')    start_time = datetime.datetime.now(datetime.timezone.utc)    expiry_time = start_time + datetime.timedelta(days=1)    sas_token = generate_blob_sas(account_name=account_name,                              container_name=container_name,                              blob_name=file_name,                              account_key=account_key,                              permission=BlobSasPermissions(read=True),                              expiry=expiry_time)    sas_url = f"https://{account_name}.blob.core.windows.net/{container_name}/{file_name}?{sas_token}"    return sas_url


I am using above function to generate sas url its working for blob name which doesn't have space , but its not working when a blob name contains space

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

1 answer

Sort by: Most helpful
  1. KarishmaTiwari-MSFT 20,772 Reputation points Microsoft Employee Moderator
    2023-06-28T01:17:41.86+00:00

    @Rishabha Dwivedi
    Thanks for posting your query on Microsoft Q&A.

    You can resolve this by encoding your file name using a standard URL encoding function like urllib.parse.quote()
    Reference documentation: https://docs.python.org/3/library/urllib.parse.html#url-quoting

    Please try the above and let me know the results in the comments. Thanks.


Your answer

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