【问题标题】:Azure: Resource not found in blob_clientAzure:在 blob_client 中找不到资源
【发布时间】:2020-12-09 10:53:56
【问题描述】:

我正在尝试使用 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 的更好方法是什么?

【问题讨论】:

  • 请不要这样做...我会更新答案。

标签: azure azure-active-directory azure-functions azure-blob-storage


【解决方案1】:
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 名称替换为动态值。)

看看官方文档:

https://docs.microsoft.com/en-us/python/api/azure-storage-blob/azure.storage.blob.blobserviceclient?view=azure-python

https://docs.microsoft.com/en-us/python/api/azure-storage-blob/azure.storage.blob.blobclient?view=azure-python#methods

当你面对NoneType object has no attribute 'rstrip'时,这意味着你应该给方法一个连接字符串。

【讨论】:

  • 谢谢,只是有一点点改变:代替 os.getenv('str'),直接提供连接字符串的值对我有用。前者给出了NoneType object has no attribute 'rstrip'的错误。
  • @nipan09 这个问题应该来自from_connection_string()方法中的参数为null。你已经给出了连接字符串吗?它应该来自这个地方:i.stack.imgur.com/O6ZR6.png
  • 我已经在我的环境变量中设置了那个连接字符串,我想这就是它起作用的原因。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-11
  • 2020-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多