我正在尝试为存储帐户构建备份系统。只是为了使示例尽可能清晰,这是我的存储帐户结构
storageA:
|---container1
| |---Blob1.txt
|---container2
| |---Blob2.txt
| |---Blob3.txt
| |---Blob4.txt
|---container3
|---Blob5.txt
我有一个脚本可以遍历容器和 blob,并将相同的结构复制到另一个存储帐户。脚本如下。
from typing import Container
from azure.storage.blob import BlobClient, BlobServiceClient, ContainerClient
from azure.storage.blob import ResourceTypes, AccountSasPermissions
from azure.storage.blob import generate_account_sas
from datetime import *
today = str(datetime.now().date())
print(today)
#================================ SOURCE ===============================
# Source Client
connection_string = '' # The connection string for the source container
account_key = '' # The account key for the source container
source_container_name = 'newblob' # Name of container which has blob to be copied
# Create client
client = BlobServiceClient.from_connection_string(connection_string)
client = BlobServiceClient.from_connection_string(connection_string)
all_containers = client.list_containers(include_metadata=True)
for container in all_containers:
# 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= AccountSasPermissions(read=True,list=True),
# start = datetime.now(),
expiry = datetime.utcnow() + timedelta(hours=4) # Token valid for 4 hours
)
print(container['name'], container['metadata'])
# print("==========================")
container_client = client.get_container_client(container.name)
# print(container_client)
blobs_list = container_client.list_blobs()
for blob in blobs_list:
# Create blob client for source blob
source_blob = BlobClient(
client.url,
container_name = container['name'],
blob_name = blob.name,
credential = sas_token
)
print(blob.name)
print("==========================")
# # ============================= TARGET =======================================
# Target Client
target_connection_string = ''
target_account_key = ''
source_container_name = source_container_name
target_blob_name = blob.name
target_destination_blob = container['name'] + today
print(target_destination_blob)
# Create target client
target_client = BlobServiceClient.from_connection_string(target_connection_string)
container = ContainerClient.from_connection_string(target_connection_string, target_destination_blob)
try:
container_client = target_client.create_container(target_destination_blob)
# Create new blob and start copy operation.
except:
new_blob = target_client.get_blob_client(target_destination_blob, target_blob_name)
new_blob.start_copy_from_url(source_blob.url)
# print(source_blob.url)
此脚本制作容器和 blob 的完整副本,没有任何错误。但是当我去我的target
存储时,我可以看到我有相同的容器,容器 1 和 3 它们有正确的 blob,但容器 2 只有 2 个 blob,无论我是否尝试将新文件上传到源存储并运行我的脚本,但新文件永远不会被复制。
谁能帮我理解这个问题?非常感谢
更新:经过一些调试,我发现了一些有趣的东西。在我的代码块中,我放置了一些打印语句来跟踪发生的循环,特别是在复制 blob 时。
这是我的代码的更新版本,重现:
from typing import Container
from azure.storage.blob import BlobClient, BlobServiceClient, ContainerClient
from azure.storage.blob import ResourceTypes, AccountSasPermissions
from azure.storage.blob import generate_account_sas
from datetime import *
today = str(datetime.now().date())
print(today)
#================================ SOURCE ===============================
# Source Client
connection_string = '' # The connection string for the source container
account_key = '' # The account key for the source container
# source_container_name = 'newblob' # Name of container which has blob to be copied
# Create client
client = BlobServiceClient.from_connection_string(connection_string)
client = BlobServiceClient.from_connection_string(connection_string)
all_containers = client.list_containers(include_metadata=True)
for container in all_containers:
# 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= AccountSasPermissions(read=True,list=True),
# start = datetime.now(),
expiry = datetime.utcnow() + timedelta(hours=4) # Token valid for 4 hours
)
print(container['name'], container['metadata'])
print("==========================")
# print("==========================")
container_client = client.get_container_client(container.name)
# print(container_client)
blobs_list = container_client.list_blobs()
for blob in blobs_list:
# Create blob client for source blob
source_blob = BlobClient(
client.url,
container_name = container['name'],
blob_name = blob.name,
credential = sas_token
)
print(blob.name)
# # ============================= TARGET =======================================
# Target Client
target_connection_string = ''
target_account_key = ''
source_container_name = container['name']
target_blob_name = blob.name
target_destination_blob = container['name'] + today
print(target_destination_blob)
# Create target client
target_client = BlobServiceClient.from_connection_string(target_connection_string)
container = ContainerClient.from_connection_string(target_connection_string, target_destination_blob)
try:
container_client = target_client.create_container(target_destination_blob)
new_blob = target_client.get_blob_client(target_destination_blob, target_blob_name)
new_blob.start_copy_from_url(source_blob.url)
print(f"TRY: saving blob {target_blob_name} into {target_destination_blob} ")
except:
# Create new blob and start copy operation.
new_blob = target_client.get_blob_client(target_destination_blob, target_blob_name)
new_blob.start_copy_from_url(source_blob.url)
print(f"TRY: saving blob {target_blob_name} into {target_destination_blob} ")
# print(source_blob.url)
现在,当我运行代码时,我得到以下输出:
==========================
blob1 {}
lastfile.txt
blob12021-09-22
COPY TO
EXCEPT: saving blob lastfile.txt into blob12021-09-22
==========================
blob2 {}
lastfile.txt
lastupdate.txt
newFile.txt
blob22021-09-22
COPY TO
EXCEPT: saving blob newFile.txt into blob22021-09-22
==========================
blob3 {}
lastupdate.txt
blob32021-09-22
COPY TO
EXCEPT: saving blob lastupdate.txt into blob32021-09-22
如我所见,整个循环仅复制列表的最后一个文件。这是我对多循环感到困惑的地方。请任何人解释我做错了什么,以及如何使循环针对每个文件并将其复制到新存储?非常感谢您为我提供的任何帮助