0
votes

I'm trying to copy a blob from Azure storage blob container to a file share, running the following script on Azure Databricks

dbutils.library.installPyPI('azure-storage-blob')
dbutils.library.installPyPI('azure-storage-file-share')
from azure.storage.blob import BlobServiceClient, BlobClient
from azure.storage.fileshare import ShareClient, ShareFileClient

connection_string = my_connection_string

blobserviceclient = BlobServiceClient.from_connection_string(connection_string) 
source_blob = BlobClient(blobserviceclient.url,container_name = 'my-container-name', blob_name = 'my_file.json')

fileshareclient = ShareClient.from_connection_string(connection_string, 'my-fileshare-name')
destination_file= fileshareclient.get_file_client('my_file.json')

destination_file.start_copy_from_url(source_blob.url)

I get the following error:

ResourceNotFoundError: The specified resource does not exist.

When I check for source_blob.url and destination_file.url, they both exist:

source_blob.url
'https://myaccountname.file.core.windows.net/my-container-name/my_file.json'

and

destination_file.url
'https://myaccountname.file.core.windows.net/my-fileshare-name/my_file.json'

I used the examples from this: https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/storage/azure-storage-file-share/samples/file_samples_client.py Any idea what I'm doing wrong? This works when I use AzCopy. I can also copy from one blob container to another, just not from the blob container to file share.

1
You would need to create a SAS URL for the source blob and use that in your start_copy_from_url method.Gaurav Mantri
I'm using the account key in the connection string, though. Isn't that enough?Saba Far
@SabaFar, when copying, the resource must be public or using url+sastoken. If the answer is helpful, please accept it as answer. Thanks:).Ivan Yang

1 Answers

0
votes

You should use sasToken with the blob url when using the method start_copy_from_url or set the source blob container as public. Otherwise, it will throw the error you've seen.

For sasToken, you can generate it from code or from azure portal.

Here is the sample code including generating sas token for blob:

from azure.storage.blob import BlobServiceClient, BlobClient, generate_blob_sas, BlobSasPermissions
from azure.storage.fileshare import ShareClient, ShareFileClient
from datetime import datetime, timedelta

connection_string="xxx"

blobserviceclient = BlobServiceClient.from_connection_string(connection_string)
source_blob = BlobClient(blobserviceclient.url,container_name="xxx", blob_name="xxx")

#generate sas token for this blob
sasToken = generate_blob_sas(
    account_name="xxx",
    container_name="xxx",
    blob_name="xxxx",
    account_key="xxx",
    permission= BlobSasPermissions(read=True),
    expiry=datetime.utcnow() + timedelta(hours=1)
)

fileshareclient =ShareClient.from_connection_string(connection_string,"xxx")
destination_file = fileshareclient.get_file_client('xxx')

destination_file.start_copy_from_url(source_blob.url+"?"+sasToken)

print("**copy completed**")