首页 > 解决方案 > 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”的内容?

标签: pythonazure-functionsazure-blob-storage

解决方案


system-assigned-identity 还需要角色“Storage Queue Data Contributor”。并且要在日志中显示文件的内容,“download_stream”应该由 download_stream.readall() 替换。


推荐阅读