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

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

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

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

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


<details>
<summary>英文:</summary>

**I tried in my environment and got the below results:**

&gt; 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 = &quot;Your storage connection string&quot;
    container_name = &quot;test3&quot;
    blob_name = &quot;sample1.xlsx&quot;
    new_blob_name = &quot;sample567.xlsx&quot;
    
    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(&quot;The blob is Renamed successfully:&quot;,{new_blob_name})


**Output:**

    The blob is Renamed successfully: {&#39;sample567.xlsx&#39;}
    
![enter image description here](https://i.stack.imgur.com/or2qI.png)

**Portal:**
![enter image description here](https://i.stack.imgur.com/AM265.png)

</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:

确定