如何解决使用 azure python sdk
我正在尝试删除非托管磁盘的非托管快照(操作时间小于 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
只会向我显示快照的时间戳。
从门户我可以删除单个快照,所以一定有办法。
解决方法
这看起来很傻,但我必须使用单独的类为 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)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。