英文:
How to rename an already existing blob in azure storage explorer using python
问题
我想要在Azure存储资源管理器中重命名一个 blob 文件。它位于特定 blob 容器的文件夹中。我正在使用 python。
我使用了这段代码。
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)
但是在 azure.storage.blob 中没有 blob_service 模块。我尝试过使用 BlockBlobService,但无法导入它们。
有人能告诉我如何使用 python 重命名已存在的 blob 吗?
英文:
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?
答案1
得分: 0
我在我的环境中尝试并获得了以下结果:
> 请问有人能告诉我如何使用Python重命名已经存在的Blob吗?
最初,我在Azure Blob存储中有一个名为**`sample1.xlsx`**的xlsx文件。
**Portal:**
![在此输入图像描述](https://i.stack.imgur.com/eSERq.png)
您可以使用以下代码来重命名文件名。
**代码:**
```python
from azure.storage.blob import BlobServiceClient
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)
# 复制Blob到新名称
new_blob_client.start_copy_from_url(blob_client.url)
# 删除原始Blob
blob_client.delete_blob()
print("Blob已成功重命名:", {new_blob_name})
输出:
Blob已成功重命名: {'sample567.xlsx'}
Portal:
<details>
<summary>英文:</summary>
**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:**
![enter image description here](https://i.stack.imgur.com/eSERq.png)
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'}
![enter image description here](https://i.stack.imgur.com/or2qI.png)
**Portal:**
![enter image description here](https://i.stack.imgur.com/AM265.png)
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论