使用 Python SDK 将 azure blob 复制到 azure fileshare

copying azure blob to azure fileshare, using Python SDK

我正在尝试将 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 容器,而不是从 blob 容器复制到文件共享。

在使用 start_copy_from_urlset the source blob container as 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**")