使用Python SDK将azure blob复制到azure文件共享



我正在尝试将一个blob从Azure存储blob容器复制到文件共享,在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)

我得到以下错误:

ResourceNotFoundError: The specified resource does not exist.

当我检查source_blob.url和destination_file.url时,它们都存在:

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

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

我使用了以下示例:https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/storage/azure-storage-file-share/samples/file_samples_client.py知道我做错了什么吗?这在我使用AzCopy时有效。我也可以从一个blob容器复制到另一个,只是不能从blob容器复制文件共享。

使用方法start_copy_from_url或将源blob容器设置为public时,应将sasToken与blob url一起使用。否则,它将抛出您看到的错误。

对于sasToken,您可以从代码或azure门户生成它。

以下是示例代码,包括为blob:生成sas令牌

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**")

最新更新