0
votes

I am trying to upload a blob using Jupyter Notebook to Azure Storage. I have a sample code as below:

from azure.storage.blob import BlockBlobService

accountName = "< account name>"
ContainerSAS = "<SAS key>"
containerName = "< container name>"

try:
    sas_service = BlockBlobService(account_name=accountName, sas_token=ContainerSAS)
except Exception as e:
    print("There was an error during SAS service creation. Details: {0}".format(e))

from azure.storage.blob import ContentSettings

blobName = "< blob name >"
try:
    sas_service.create_blob_from_path(
       'accountName',
       'blobName',
       'Chicago_Crime_Data-v2.csv',
       content_settings=ContentSettings(content_type='Chicago_Crime_Data-v2/csv')
    )
except Exception as e:
    print("There was an error during blob uploading. Details: {0}".format(e))

But I am getting an error which says: Details: Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.

I am not able to understand what is wrong here. I am a newbie. Any help would be really appreciated. Thanks.

Edit: Should I provide the account name, container name and key?

3
can you solve the issue by my post? if you still have issue, please let me know.Ivan Yang

3 Answers

1
votes

Please make sure you generate the sas token as per this screenshot:

enter image description here

There is also an error in the sas_service.create_blob_from_path method, the first parameter should be container_name, not account_name

The following code works well at my side:

from azure.storage.blob import BlockBlobService, ContentSettings

accountName = "xxx"
ContainerSAS = "xxx"
containerName = "test4"

sas_service = BlockBlobService(account_name=accountName,sas_token=ContainerSAS)

blobname = "222.PNG"

sas_service.create_blob_from_path(containerName,blobname,"F:\\azure stack overflow\\2019\\09\\30\\disk.PNG")

print("done")

Test result:

enter image description here

0
votes

Kindly verify whether your account Key is right and also ensure that your system time is set correctly , If your system time is set incorrect you might get "Failed to Authenticate" error.

0
votes

My local file path was "./rootfolder" and then when it found a file to upload, it would be like file="./rootfolder/folder/file.dcm"

so then I tried to upload that file wholesale:

blob_client = blob_service_client.get_blob_client(container=container_name, blob=file)

And then I got the following error:

Azure Server failed to authenticate the request

and

AuthenticationErrorDetail:The MAC signature found in the HTTP request '' is not the same as any computed signature. Server used following string to sign: 'PUT...

I found you had to something like this instead, to remove that ./ from the file path.

file_path_on_azure = file_name.replace("./", "")
 
blob_client = blob_service_client.get_blob_client(container=container_name, blob=file_path_on_azure)