Search code examples
azure-sdk-python

Azure python sdk: copy blob from SAS token


i am trying to copy a disk snapshot to a storage account programatically.

from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient

connString= "storageaccount connection string"
blob_client = BlobClient(connString,container_name,'test1')

copy_source_url =r"SASTOKEN"

blob_client.start_copy_from_url(copy_source_url)

I run in to this issue:

[Errno 11001] getaddrinfo failed

I found out i can reproduce the error with this piece of code:

import socket
try:
    print(socket.getaddrinfo(copy_source_url,80))
except Exception, e:
    print(type(e))
    print(e)

I played around for a while, and found out i can get succesfully run socket.getaddrinfo with the base url (md-.blob.core.windows.net) but when i include the rest of the sas token it does not work.

Is there something i need to do to make the sas url work in python (I can download the file by using the link in a browser), or is it possible to let tell the SDK to skip this getaddrinfo?

Oh and i tried both port 80 and 443 by the way.

EDIT: This happens both when running the code locally, or running through an azure automation account. I am using python 2.7 to support automation accounts.


Solution

  • I test it in my side and find there are something wrong in your code. Since we need to copy the content of a source file to the target file in storage, so we can't get the blob_client as below which your code shows:

    blob_client = BlobClient(connString,container_name,'test1')
    

    We need to write the content of source file to the target file("test1"), so we need the "Write" permission to it. So we should generate the SAS url of "test1" which contains "Write" permission (In my screenshot, I create a the target file named "file1.csv" for test and I select all of the four permissions). enter image description here

    Then use the code below to get blob_client:

    blob_client = BlobClient.from_blob_url("SAS url of file1.csv which we generated above")
    

    Now we need to generate the SAS url of the source file(I created a "file2.csv" as the source file) in storage, it just need the "Read" permission. And then start copy from the url.

    copy_source_url = r"SAS url of the source file which we generated"
    
    blob_client.start_copy_from_url(copy_source_url, metadata=None, incremental_copy=False)
    

    After that, the code works fine to copy the content of the source file("file2.csv") to the target file("file1.csv"). Below is all of my code for your reference:

    enter image description here