azure - Golang aztables az登录授权失败

标签 azure go azure-table-storage

我正在尝试按照 https://learn.microsoft.com/en-us/azure/cosmos-db/table/how-to-use-go?tabs=bash 上的教程进行操作,并且我从 azidentity.NewDefaultAzureCredential 获得的凭据能够创建一个表,因此它具有足够的角色,但是当它调用 client.AddEntity 时,我得到403 错误。

我使用 az login 登录我的根级别帐户,它具有全局管理员角色,并且具有足够的权限来添加表,但没有足够的权限来添加实体。我需要在表或存储帐户上进行不同的配置吗?

    cred, err := azidentity.NewDefaultAzureCredential(nil)
    if err != nil {
        panic(err)
    }
    serviceURL := fmt.Sprintf("https://%s.table.core.windows.net/%s", accountname, tablename)
    client, err := aztables.NewClient(serviceURL, cred, nil)
    if err != nil {
        panic(err)
    }
    _, err := client.CreateTable(context.TODO(), nil)
    if err != nil {
        panic(err)
    }
        myEntity := InventoryEntity{
        Entity: aztables.Entity{
            PartitionKey: "pk001",
            RowKey:       "rk001",
        },
        Price:       3.99,
        Inventory:   20,
        ProductName: "Markers",
        OnSale:      false,
    }

    marshalled, err := json.Marshal(myEntity)
    if err != nil {
        panic(err)
    }

    _, err = client.AddEntity(context.TODO(), marshalled, nil)
    if err != nil {
        panic(err)
    }

最后一行是失败的,尽管在此之前我能够连接并创建表。同一帐户随后也可以删除该表。

最佳答案

我在我的环境中进行了尝试并得到了以下结果:

最初,我尝试使用相同的代码并得到类似的错误:

RESPONSE493: 493 Forbidden ERRORCODE: AuthorizationPermissionMismatch “odata.error"™:{ “code": “AuthorizationPermissionMismatch", i okcs-y-|1 “lang”: “en-US", “value”: "This request is not authorized to perform this operation using this permission.\nRequestId:fb@70c8d-4902-@0e2-1bed-db23 97000000\nTime:2023-08-30T@6:44:4xxx" goroutine 1 [running]:

enter image description here

当您的用户可能没有向表添加实体的必要权限时,就会出现上述错误。

您需要为您的用户分配存储表数据贡献者角色

enter image description here

您可以使用以下代码使用 Golang 创建表并添加实体。

代码:

package main

import (
    "context"
    "encoding/json"
    "fmt"

    "github.com/Azure/azure-sdk-for-go/sdk/azidentity"
    "github.com/Azure/azure-sdk-for-go/sdk/data/aztables"
)

type InventoryEntity struct {
    aztables.Entity
    Price       float64 `json:"Price"`
    Inventory   int     `json:"Inventory"`
    ProductName string  `json:"ProductName"`
    OnSale      bool    `json:"OnSale"`
}

func main() {
    accountName := "venkat123"
    tableName := "table1"

    
    cred, err := azidentity.NewDefaultAzureCredential(nil)
    if err != nil {
        panic(err)
    }
    serviceURL := fmt.Sprintf("https://%s.table.core.windows.net/%s", accountName, tableName)
    client, err := aztables.NewClient(serviceURL, cred, nil)
    if err != nil {
        panic(err)
    }

    // Create the table
    _, err = client.CreateTable(context.Background(), nil)
    if err != nil {
        panic(err)
    }

    // Add an entity to the table
    myEntity := InventoryEntity{
        Entity: aztables.Entity{
            PartitionKey: "pk001",
            RowKey:       "rk001",
        },
        Price:       3.99,
        Inventory:   20,
        ProductName: "Markers",
        OnSale:      false,
    }

    marshalled, err := json.Marshal(myEntity)
    if err != nil {
        panic(err)
    }

    _, err = client.AddEntity(context.Background(), marshalled, nil)
    if err != nil {
        panic(err)
    }
}

门户:

enter image description here

引用:

Use the Azure Table client library for Go | Microsoft Learn

关于azure - Golang aztables az登录授权失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76932327/

相关文章:

http - 禁用从服务器接收到的带有错误 header 的 HTTP 正文的解压缩

Azure 存储表副本

go - 打印/访问超出范围的 slice 索引时不要 panic

azure - Azure 表中的排名

c# - Windows Azure 表存储中的批量删除

azure - Databricks PAT token 和 secret 创建

使用用户分配的托管ID的 azure 函数 - 无法写入存储队列

.net - 了解 Azure 消息队列中的有害消息处理并在逻辑应用中使用它

bash - Git 不使用 SSH key 对 Azure DevOps 进行身份验证

go - Go 库的 BigQuery 行插入失败