如何使用Python在Azure存储资源管理器中重命名已存在的 Blob。

huangapple go评论115阅读模式
英文:

How to rename an already existing blob in azure storage explorer using python

问题

我想要在Azure存储资源管理器中重命名一个 blob 文件。它位于特定 blob 容器的文件夹中。我正在使用 python

我使用了这段代码。

  1. sourceFileName = abc.xlsx
  2. destinationFileName = xyz.xlsx
  3. sourcePath = 'cloudops-resources/outputs/'
  4. destinationPath = 'cloudops-resources/outputs/'
  5. blob_url = blob_service.make_blob_url(sourcePath, sourceFileName)
  6. 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.

  1. sourceFileName = abc.xlsx
  2. destinationFileName = xyz.xlsx
  3. sourcePath = 'cloudops-resources/outputs/'
  4. destinationPath = 'cloudops-resources/outputs/'
  5. blob_url = blob_service.make_blob_url(sourcePath,sourceFileName)
  6. 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

  1. 我在我的环境中尝试并获得了以下结果:
  2. > 请问有人能告诉我如何使用Python重命名已经存在的Blob吗?
  3. 最初,我在Azure Blob存储中有一个名为**`sample1.xlsx`**的xlsx文件。
  4. **Portal:**
  5. ![在此输入图像描述](https://i.stack.imgur.com/eSERq.png)
  6. 您可以使用以下代码来重命名文件名。
  7. **代码:**
  8. ```python
  9. from azure.storage.blob import BlobServiceClient
  10. connection_string = "您的存储连接字符串"
  11. container_name = "test3"
  12. blob_name = "sample1.xlsx"
  13. new_blob_name = "sample567.xlsx"
  14. blob_service_client = BlobServiceClient.from_connection_string(connection_string)
  15. blob_client = blob_service_client.get_blob_client(container_name, blob_name)
  16. new_blob_client = blob_service_client.get_blob_client(container_name, new_blob_name)
  17. # 复制Blob到新名称
  18. new_blob_client.start_copy_from_url(blob_client.url)
  19. # 删除原始Blob
  20. blob_client.delete_blob()
  21. print("Blob已成功重命名:", {new_blob_name})

输出:

  1. Blob已成功重命名: {'sample567.xlsx'}

如何使用Python在Azure存储资源管理器中重命名已存在的 Blob。

Portal:
如何使用Python在Azure存储资源管理器中重命名已存在的 Blob。

  1. <details>
  2. <summary>英文:</summary>
  3. **I tried in my environment and got the below results:**
  4. &gt; Can someone please tell me how to rename an already existing blob using python?
  5. Initially, I have an xlsx file with the name **`sample1.xlsx`** in azure blob storage.
  6. **Portal:**
  7. ![enter image description here](https://i.stack.imgur.com/eSERq.png)
  8. You can use the below code to rename the file name.
  9. **Code:**
  10. from azure.storage.blob import BlobServiceClient
  11. connection_string = &quot;Your storage connection string&quot;
  12. container_name = &quot;test3&quot;
  13. blob_name = &quot;sample1.xlsx&quot;
  14. new_blob_name = &quot;sample567.xlsx&quot;
  15. blob_service_client = BlobServiceClient.from_connection_string(connection_string)
  16. blob_client = blob_service_client.get_blob_client(container_name, blob_name)
  17. new_blob_client = blob_service_client.get_blob_client(container_name, new_blob_name)
  18. # Copy the blob to the new name
  19. new_blob_client.start_copy_from_url(blob_client.url)
  20. # Delete the original blob
  21. blob_client.delete_blob()
  22. print(&quot;The blob is Renamed successfully:&quot;,{new_blob_name})
  23. **Output:**
  24. The blob is Renamed successfully: {&#39;sample567.xlsx&#39;}
  25. ![enter image description here](https://i.stack.imgur.com/or2qI.png)
  26. **Portal:**
  27. ![enter image description here](https://i.stack.imgur.com/AM265.png)
  28. </details>

huangapple
  • 本文由 发表于 2023年3月31日 16:29:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/75896403.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定