【问题标题】:Delete single snapshot in unmanaged disk with azure python sdk使用 azure python sdk 删除非托管磁盘中的单个快照
【发布时间】: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


    【解决方案1】:

    这似乎很愚蠢,但我必须使用单独的类为 blob 创建 另一个 客户端,但它似乎可以工作。我在代码中有 3 个左右的存储帐户客户端只是为了列出您认为其中一个可以删除快照的 blob?

    我更新了 try 块以使用 BlobClient,然后指定快照 ID。

    from azure.storage.blob import BlobServiceClient, RetentionPolicy, BlobClient
    
    try:
        blob_client = BlobClient(account_url=oauth_url, container_name=container.name, blob_name=blob.name, snapshot=blob.snapshot, credential=default_credential)
        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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-08
      • 2021-11-14
      • 2017-12-31
      • 2018-05-25
      • 2018-03-28
      • 1970-01-01
      • 2020-02-17
      • 1970-01-01
      相关资源
      最近更新 更多