我正在尝试使用 python 脚本删除我的 azure blob,为此我使用 azure.storage 库中的 BlobClient。
这是代码,我正在尝试运行:
import json, sys, os, time
from azure.storage.blob import BlobServiceClient, PublicAccess, BlobClient
endpoint = os.environ['COMPUTER_VISION_ENDPOINT']
subscription_key = os.environ['COMPUTER_VISION_SUBSCRIPTION_KEY']
text_recognition_url = endpoint + "/vision/v3.0/read/analyze"
file_name = input("Enter the file name: ")
image_url = "https://textimage.blob.core.windows.net/handwritten-text/" + file_name
blob_client = BlobClient.from_blob_url(blob_url=image_url)
blob_client.delete_blob()
print("Done")
虽然 blob 和容器存在于 azure 存储帐户中,但它会引发以下错误:
azure.core.exceptions.ResourceNotFoundError: The specified resource does not exist.
我哪里错了?
使用 python 脚本删除特定打开的 blob 的更好方法是什么?
最佳答案
import os
from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient
blob_service_client = BlobServiceClient.from_connection_string(os.getenv('str'))
container_client = blob_service_client.get_container_client("test")
blob_client = blob_service_client.get_blob_client("test", "test.txt")
blob_client.delete_blob()
上面的代码在我这边工作得很好。(您可以用动态值替换容器名称和 blob 名称。)
看看官方文档:
当您遇到NoneType对象没有属性“rstrip”
时,这意味着您应该为该方法提供连接字符串。
关于Azure:在 blob_client 中找不到资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63499378/