我指的是https://learn.microsoft.com/en-us/azure/storage/blobs/storage-quickstart-blobs-python文件。我还没有找到将文件从一个容器复制/移动到另一个容器的适当API。假设我有两个容器A和B,现在我想把一个blob从A复制到B,我怎样才能做到这一点?举一个例子会更好。
图书馆详情:
azure-core==1.1.1
azure-storage-blob==12.0.0
注意:我已经通过了这条线,它只支持较早版本的SDK。
发布于 2019-12-04 05:04:25
下面是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)
有关如何获取容器的连接字符串和访问键的更多信息,请参见这里。
这个答案假设两个容器都在相同的订阅中。
发布于 2019-12-04 04:47:09
您应该看看SDK中的start_copy_from_url
方法。
来自同一链接:
# 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)
https://stackoverflow.com/questions/59175743
复制