【发布时间】:2021-01-21 20:00:01
【问题描述】:
我正在尝试删除非托管磁盘的非托管快照(cteaion 时间小于 X)。我能看到的唯一选择是删除所有快照并保留磁盘或一起删除磁盘和快照。
我正在使用的代码:
from datetime import datetime, timdelta
from azure.identity import DefaultAzureCredential
from azure.storage.blob import BlobServiceClient, RetentionPolicy
blob_service_client = BlobServiceClient(
account_url="https://{}.blob.core.windows.net".format("myStorageAccount"),
credential=DefaultAzureCredential(),
)
containers = blob_service_client.list_containers(include_metadata=True)
for container in containers:
container_client = blob_service_client.get_container_client(container.name)
blobs = container_client.list_blobs(include=["snapshots", "metadata"])
for blob in blobs:
if blob.name.endswith(".vhd"):
if blob.snapshot:
# Do some checks
if blob.creation_time < datetime.now(blob.creation_time.tzinfo) - timedelta(days=30)
try:
blob_client = container_client.get_blob_client(blob)
blob_client.delete_blob()
print(f"Deleted - {blob.name} in {container.name} from {storage_account.name} in subscription {subscription_id} - criteria passed")
except Exception as error:
print(error)
但是,我收到错误消息,它试图删除父磁盘并且无法删除它,因为它有快照。但是,我想做的只是删除满足创建时间检查的单个快照。
This operation is not permitted because the blob has snapshots.
RequestId:<REDACTED>
Time:2021-01-21T11:29:06.9708873Z
ErrorCode:SnapshotsPresent
Error:None
我似乎可以删除快照的唯一方法是指定客户端删除所有与磁盘相关的快照
blob_client.delete_blob(delete_snapshots="only")
似乎即使我正在使用的 blob 项目与快照相关,但 blob 的名称仍然是原始磁盘。引用 blob.snapshot 只会向我显示快照的时间戳。
我可以从门户中删除单个快照,所以一定有办法。
【问题讨论】:
标签: python azure azure-blob-storage