0

When looking at the possible event types for an EventGrid Subscription, one is Blob Renamed.

enter image description here

How is this event triggered when using the Azure Python SDK?

I don't see a Rename Blob method in the SDK.

8
  • You mean you want to submit the event to eventgrid? Commented Apr 23, 2024 at 16:23
  • Hello. I want the event to automatically be created when a Blob is renamed. This EventGrid System Topic is monitoring a Blob Storage Account. The Blob Renamed event type is under the Filters tab on the EventGrid Subscription I'm configuring. Commented Apr 23, 2024 at 17:07
  • 1
    As per the documentation. Triggered when a blob is renamed. Specifically, this event is triggered when clients use the RenameFile operation that is available in the Azure Data Lake Storage Gen2 REST API. Commented Apr 23, 2024 at 17:23
  • I don't want to consume the event, but rather create the event using the Azure Storage Python SDK. I can't find a method in the [BlobClient docs ](learn.microsoft.com/en-us/python/api/azure-storage-blob/…) for Rename-anything. Is this functionality NOT available in the Python SDK? Would be undesirable to switch to REST API using requests library when I'm using built-in methods otherwise. Commented Apr 23, 2024 at 18:06
  • 1
    In my previous comment I mean to create the event(sorry for the typo). To do that I think your storage account has to be create with Azure Data Lake Storage Gen2 Commented Apr 23, 2024 at 18:11

1 Answer 1

0

How to trigger the Blob Renamed EventGrid Event using Azure Python SDK?

I agree with user459872's comment that a normal v2 storage account does not seem to support the rename operation with Azure Blob Storage Python SDK.

To trigger this event using the Azure Python SDK, you can use the code below to rename a blob using the Azure Blob Storage Python SDK. This will copy the blob to a new blob with the new name and then delete the old blob.

Code:

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})

Output:

The blob is renamed successfully: {'example.png'}

enter image description here

Reference: How to rename an already existing blob in azure storage explorer using python - Stack Overflow.

Sign up to request clarification or add additional context in comments.

4 Comments

Right, but this will trigger the CreateBlob event in EventGrid, NOT the RenameBlob event.
As you menioned in comment data lake storage account you can refer the learn.microsoft.com/en-us/azure/storage/blobs/…
Here it will rename the folder and here filesystem is your blob container.
Here is my reference in datalake storage account to rename stackoverflow.com/questions/78371091/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.