我想通过使用来自活动目录服务主体的凭据从 python 访问私有 blob 存储。
我知道这个相关问题How do I authenticate a user against an Azure storage blob in python? 这帮助我走到了这一步,但现在我陷入了困境。
我可以进行身份验证并获取令牌,该令牌允许我列出容器、创建新容器,但不允许我列出或访问任何 blob。
我希望通过 cli 进行设置az。
服务主体的设置如下:
az ad sp create-for-rbac -n "http://$NAME" --role Contributor \
--scopes "/subscriptions/$SUB_ID/resourceGroups/$RESOURCE_GROUP"
Run Code Online (Sandbox Code Playgroud)
我认为应该提供完全访问权限,但我还添加了以下内容以确保:
az role assignment create \
--role "Storage Blob Data Contributor" \
--assignee-object-id "$OBJECT_ID" \
--assignee-principal-type "ServicePrincipal" \
--scope "/subscriptions/$SUB_ID/resourceGroups/$RESOURCE_GROUP/providers/Microsoft.Storage/storageAccounts/$STORAGE_ACCOUNT/blobServices/default/containers/$CONTAINER"
Run Code Online (Sandbox Code Playgroud)
然后我像这样进行身份验证:
from azure.common.credentials import ServicePrincipalCredentials
import adal
from azure.storage.blob import (
BlockBlobService,
ContainerPermissions,
)
from azure.storage.common import (
TokenCredential
)
# …Run Code Online (Sandbox Code Playgroud) 我有一个 Azure Functions (Python 3) 函数,它从服务总线队列中获取一条消息,并因此在容器中创建一个 Blob。
函数触发器是服务总线消息。此消息是具有多个属性的 JSON 对象,其中之一是 blob 名称。
{
"name": "outputblob",
"type": "blob",
"path": "samples-workitems/{queueTrigger}-Copy",
"connection": "MyStorageConnectionAppSetting",
"direction": "out"
}
Run Code Online (Sandbox Code Playgroud)
但这表明触发消息只包含 blob 名称。我不能将消息单独设为 blob 名称,因为我需要消息中的其他属性来确定要执行的操作/将哪些数据放入 blob。
有什么方法可以使用输出绑定来解决这个问题吗?
谢谢。