python - Python Function App 尝试读取文件时的 AuthorizationPermissionMismatch
问题描述
当我尝试使用函数应用程序从 blob 存储读取 txt 文件时,它会在日志中返回此错误:
结果:失败异常:HttpResponseError:此请求无权使用此权限执行此操作。RequestId:00000-0000-00000-00000-000000000000 时间:2021-07-28T13:14:46.7803762Z 错误代码:AuthorizationPermissionMismatch 错误:无
在存储帐户的访问控制菜单中,已将“存储 Blob 数据参与者”角色分配给函数应用的系统分配标识。
这是我的代码:
import logging
import azure.functions as func
from azure.storage.blob import BlobServiceClient, BlobClient
from azure.identity import DefaultAzureCredential
def main(req: func.HttpRequest) -> func.HttpResponse:
blob_url = "https://my-storage-account.blob.core.windows.net"
blob_credential = DefaultAzureCredential()
blob_client = BlobClient(account_url=blob_url, container_name='tests', blob_name='file.txt', credential=blob_credential)
download_stream = blob_client.download_blob()
logging.info('Contents of the download_stream: %s', download_stream)
return func.HttpResponse("OK", status_code=200)
为什么我收到错误而不是“file.txt”的内容?
解决方案
system-assigned-identity 还需要角色“Storage Queue Data Contributor”。并且要在日志中显示文件的内容,“download_stream”应该由 download_stream.readall() 替换。
推荐阅读
- json - 如何使用 Visual Basic 将序列化对象放入 json 文件中?
- google-apps-script - 计算电报中收到的图像数量
- laravel - Laravel 按关系属性的平均值排序
- rust - 是否可以将返回不同结构(实现共同特征)的不同函数的返回值分配给单个变量?
- javascript - “TypeError:无法读取未定义的属性'Stream'”从 React 连接到 Athena
- properties - Spring Cloud 配置优先级属性文件
- sql - 使用连接更新许多行非常慢
- python - 使用 google colab 将相同类别的图像和 json 文件过滤到谷歌驱动器中的另一个文件夹中
- javascript - 如何制作一个从 20 到 0 并永远运行的计时器?
- sql - 将这些临时表变成一个更长的子查询(不能在 Power BI 中使用临时表)