在查看 EventGrid 订阅的可能事件类型时,其中之一是
Blob Renamed
。
使用 Azure Python SDK 时如何触发此事件?
我在 SDK 中没有看到
Rename Blob
方法。
如何使用 Azure Python SDK 触发
EventGrid 事件?Blob Renamed
我同意用户459872的评论,即普通的v2存储帐户似乎不支持使用Azure Blob Storage Python SDK进行重命名操作。
要使用 Azure Python SDK 触发此事件,您可以使用以下代码通过 Azure Blob 存储 Python SDK 重命名 Blob。这会将 blob 复制到具有新名称的新 blob,然后删除旧 blob。
代码:
from azure.storage.blob import BlobServiceClient
connection_string = "<Your connection string>"
blob_service_client = BlobServiceClient.from_connection_string(connection_string)
container_client = blob_service_client.get_container_client("test")
blob_client = container_client.get_blob_client("sample.png")
new_blob_name = "example.png"
new_blob_client = container_client.get_blob_client(new_blob_name)
new_blob_client.start_copy_from_url(blob_client.url)
blob_client.delete_blob()
print("The blob is renamed successfully:", {new_blob_name})
输出:
The blob is renamed successfully: {'example.png'}