登录
首页 >  Golang >  Go问答

使用 Golang 列举所有 Azure 容器注册表的镜像

来源:stackoverflow

时间:2024-03-23 22:18:31 146浏览 收藏

使用 Go 语言列出 Azure 容器注册表中所有镜像。首先,使用 Azure 身份验证库获取 Azure Active Directory (AAD) 令牌。然后,使用该令牌获取 ACR 刷新令牌,并使用它创建一个授权器。最后,使用授权器创建注册表客户端并获取镜像列表。

问题内容

我想通过 golang 列出来自 azure 注册表的所有映像。

我创建的是:https://github.com/azure-samples/azure-sdk-for-go-samples/tree/main/sdk/resourcemanager/containerregistry,但那里似乎没有任何帮助我。

请问有什么想法吗?

le: 我最终得到了这段代码

package main

import (
    "context"
    "encoding/json"
    "fmt"
    "log"
    "net/http"
    "net/url"

    "github.com/azure/azure-sdk-for-go/sdk/azcore/policy"
    "github.com/azure/azure-sdk-for-go/sdk/azidentity"
    "github.com/azure/azure-sdk-for-go/services/preview/containerregistry/runtime/2019-08-15-preview/containerregistry"
    "github.com/azure/go-autorest/autorest"
)

type acrtokenprovider struct {
    accesstoken string
}

func (a *acrtokenprovider) oauthtoken() string {
    return a.accesstoken
}

func newacrauthorizer() (*autorest.bearerauthorizer, error) {
    tenantid := "tenant_id"
    acrservice := "servicename.azurecr.io"
    cred, err := azidentity.newdefaultazurecredential(nil)
    if err != nil {
        panic(err)
    }
    ctx := context.background()
    aadtoken, err := cred.gettoken(ctx, policy.tokenrequestoptions{scopes: []string{"https://management.azure.com/"}})
    if err != nil {
        panic(err)
    }
    formdata := url.values{
        "grant_type":   {"access_token"},
        "service":      {acrservice},
        "tenant":       {tenantid},
        "access_token": {aadtoken.token},
    }
    jsonresponse, err := http.postform(fmt.sprintf("https://%s/oauth2/exchange", acrservice), formdata)
    if err != nil {
        panic(err)
    }
    var response map[string]interface{}
    json.newdecoder(jsonresponse.body).decode(&response)

    return autorest.newbearerauthorizer(&acrtokenprovider{accesstoken: fmt.sprint(response["refresh_token"])}), nil
}

func main() {
    client := containerregistry.newrepositoryclient("https://servicename.azurecr.io")
    authorizer, err := newacrauthorizer()
    if err != nil {
        fmt.println(err)
    }
    client.authorizer = authorizer

    // do what you need to do with client here
    attributes, err := client.getlist(context.background(), "registryname", nil)
    if err != nil {
        log.printf("error while fetching attributes, %v ", err)
    }
    fmt.print(attributes)
}

但回应是这样的

Original Error: autorest/azure: Service returned an error. Status=401 Code="Unknown" Message="Unknown service error" Details=[{"errors":[{"code":"UNAUTHORIZED","detail":[{"Action":"*","Name":"catalog","Type":"registry"}],"message":"authentication required, visit https://aka.ms/acr/authorization for more information."}]}]

我缺少什么?


正确答案


我不确定 go sdk,但您始终可以直接使用 rest api:

GET https://management.azure.com/subscriptions/{subscriptionId}/providers/Microsoft.ContainerRegistry/registries?api-version=2019-05-01

https://learn.microsoft.com/en-us/rest/api/containerregistry/registries/list?tabs=HTTP

以上就是《使用 Golang 列举所有 Azure 容器注册表的镜像》的详细内容,更多关于的资料请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>