Python 如何使用Azure blob存储SDK将blob从一个容器复制到另一个容器

Python 如何使用Azure blob存储SDK将blob从一个容器复制到另一个容器,python,azure,azure-storage,azure-blob-storage,azure-sdk-python,Python,Azure,Azure Storage,Azure Blob Storage,Azure Sdk Python,我一直在查阅这份文件。我无法找到合适的API,用于将文件从一个容器复制/移动到另一个容器。假设我有两个容器A和B。现在我想将一个blob从A复制到B。我如何实现这一点?请举个例子 图书馆详情: 注意:我已经了解了只有旧版本的SDK才支持的方法。您应该看看SDK中的方法 从同一链接: # Get the blob client with the source blob source_blob = "<source-blob-url>" copied_blob = blob_servic

我一直在查阅这份文件。我无法找到合适的API,用于将文件从一个容器复制/移动到另一个容器。假设我有两个容器A和B。现在我想将一个blob从A复制到B。我如何实现这一点?请举个例子

图书馆详情: 注意:我已经了解了只有旧版本的SDK才支持的方法。

您应该看看SDK中的方法

从同一链接:

# Get the blob client with the source blob
source_blob = "<source-blob-url>"
copied_blob = blob_service_client.get_blob_client("<target-container>", '<blob-name>')

# start copy and check copy status
copy = copied_blob.start_copy_from_url(source_blob)
props = copied_blob.get_blob_properties()
print(props.copy.status)
#获取带有源blob的blob客户端
source_blob=“”
复制的\u blob=blob\u服务\u客户端。获取\u blob\u客户端(“,”)
#开始复制并检查复制状态
复制=复制的\u blob。从\u url(源\u blob)开始\u复制\u
props=copied\u blob.get\u blob\u properties()
打印(道具、副本、状态)

以下是SDK版本12.0.0的完整示例:

from azure.storage.blob import BlobClient, BlobServiceClient
from azure.storage.blob import ResourceTypes, AccountSasPermissions
from azure.storage.blob import generate_account_sas    

connection_string = '' # The connection string for the source container
account_key = '' # The account key for the source container
source_container_name = '' # Name of container which has blob to be copied
blob_name = '' # Name of the blob you want to copy
destination_container_name = '' # Name of container where blob will be copied

# Create client
client = BlobServiceClient.from_connection_string(connection_string) 

# Create sas token for blob
sas_token = generate_account_sas(
    account_name = client.account_name,
    account_key = account_key 
    resource_types = ResourceTypes(object=True, container=True),
    permission= AccountSasPermission(read=True,list=True),
    start = datetime.now()
    expiry = datetime.utcnow() + timedelta(hours=4) # Token valid for 4 hours
)

# Create blob client for source blob
source_blob = BlobClient(
    client.url,
    container_name = source_container_name, 
    blob_name = blob_name,
    credential = sas_token
)

# Create new blob and start copy operation.
new_blob = client.get_blob_client(destination_container_name, blob_name)    
new_blob.start_copy_from_url(source_blob.url)
有关如何获取容器的连接字符串和访问密钥的更多信息,请参阅



这个答案假设两个容器都在同一个订阅中

非常感谢你的回答。这对我有用。但是,您能告诉我您是从哪里知道类变量“url”的吗?我已经跟踪了文档,但找不到它。@kanchan不客气,很高兴你发现答案有用。我必须通过检查对象找到url属性。请参阅:它需要存储帐户URI。碰巧客户端有一个带有该端点的属性,所以直接使用它很方便。是的,我可以通过转储对象来查看变量。我担心的是,由于这个类变量没有在API引用中公开,如果我的代码在将来中断会怎么样。好奇的是,在最新的SDK中,旧SDK“make_blob_url”中存在的方法是否有其他替代方法?@kanchan我理解您的担忧,但如果您不更改SDK版本,它在未来不会崩溃。无论如何,该URI指向存储源容器的存储帐户,例如
f'https://{mystorageaccount}.blob.core.windows.net/'
,因此您可以非常轻松地获取该字符串。
from azure.storage.blob import BlobClient, BlobServiceClient
from azure.storage.blob import ResourceTypes, AccountSasPermissions
from azure.storage.blob import generate_account_sas    

connection_string = '' # The connection string for the source container
account_key = '' # The account key for the source container
source_container_name = '' # Name of container which has blob to be copied
blob_name = '' # Name of the blob you want to copy
destination_container_name = '' # Name of container where blob will be copied

# Create client
client = BlobServiceClient.from_connection_string(connection_string) 

# Create sas token for blob
sas_token = generate_account_sas(
    account_name = client.account_name,
    account_key = account_key 
    resource_types = ResourceTypes(object=True, container=True),
    permission= AccountSasPermission(read=True,list=True),
    start = datetime.now()
    expiry = datetime.utcnow() + timedelta(hours=4) # Token valid for 4 hours
)

# Create blob client for source blob
source_blob = BlobClient(
    client.url,
    container_name = source_container_name, 
    blob_name = blob_name,
    credential = sas_token
)

# Create new blob and start copy operation.
new_blob = client.get_blob_client(destination_container_name, blob_name)    
new_blob.start_copy_from_url(source_blob.url)