I want to rename a blob file in Azure storage explorer. It's in a folder of a specific blob container.I'm using python.
I used this code.
sourceFileName = abc.xlsx
destinationFileName = xyz.xlsx
sourcePath = 'cloudops-resources/outputs/'
destinationPath = 'cloudops-resources/outputs/'
blob_url = blob_service.make_blob_url(sourcePath,sourceFileName)
blob_service.copy_blob(destinationPath, destinationFileName, blob_url)
But there is no blob_service module in the azure.storage.blob.I tried using BlockBlobService as well.But those can't be imported.
I referred to this and I tried using these codes as well.
Can someone please tell me how to rename an already existing blob using python?
I tried in my environment and got the below results:
Can someone please tell me how to rename an already existing blob using python?
Initially, I have an xlsx file with the name sample1.xlsx
in azure blob storage.
Portal:
You can use the below code to rename the file name.
Code:
from azure.storage.blob import BlobServiceClient
connection_string = "Your storage connection string"
container_name = "test3"
blob_name = "sample1.xlsx"
new_blob_name = "sample567.xlsx"
blob_service_client = BlobServiceClient.from_connection_string(connection_string)
blob_client = blob_service_client.get_blob_client(container_name, blob_name)
new_blob_client = blob_service_client.get_blob_client(container_name, new_blob_name)
# Copy the blob to the new name
new_blob_client.start_copy_from_url(blob_client.url)
# Delete the original blob
blob_client.delete_blob()
print("The blob is Renamed successfully:",{new_blob_name})
Output:
The blob is Renamed successfully: {'sample567.xlsx'}
Portal: