gpt4 book ai didi

azure - 使用 SDK azblob 和托管服务标识将文件从 Azure VM 上传到 Azure 存储

转载 作者:行者123 更新时间:2023-12-03 03:53:48 32 4
gpt4 key购买 nike

我正在尝试使用Go SDK for Azure storage将文件上传到azure存储容器来自附加了 Azure 托管标识的 Azure VM。我也在使用Azure auth使用 MSIConfig 创建 ServicePrincipalToken。但是我收到错误

响应状态:400 未以正确的格式给出身份验证信息。检查授权 header 的值。

有人可以帮我理解我缺少什么吗?

我使用的脚本( example 的修改形式):

// main.go
package main

import (
"log"
"fmt"
"context"
"net/url"
"strings"
"github.com/Azure/azure-storage-blob-go/azblob"
"github.com/Azure/go-autorest/autorest/azure/auth"
)

func main() {
azureServicePrincipalToken, err := auth.NewMSIConfig().ServicePrincipalToken()
if err != nil {
log.Fatal(err)
}

accountName := "<TESTSA>"
containerName := "<TESTCONTAINER>"

// Create a BlockBlobURL object to a blob in the container (we assume the container already exists).
u, _ := url.Parse(fmt.Sprintf("https://%s.blob.core.windows.net/%s/readme.txt", accountName, containerName))
credential := azblob.NewTokenCredential(azureServicePrincipalToken.Token().AccessToken, nil)
if err != nil {
log.Fatal(err)
}
blockBlobURL := azblob.NewBlockBlobURL(*u, azblob.NewPipeline(credential, azblob.PipelineOptions{}))

log.Println(blockBlobURL)

ctx := context.Background() // This example uses a never-expiring context

// Perform UploadStreamToBlockBlob
bufferSize := 2 * 1024 * 1024
maxBuffers := 3

_, err = azblob.UploadStreamToBlockBlob(ctx, strings.NewReader("Hello azblob"), blockBlobURL,
azblob.UploadStreamToBlockBlobOptions{BufferSize: bufferSize, MaxBuffers: maxBuffers})

if err != nil {
log.Fatal(err)
}
}

当我执行go run main.go时,我收到以下错误:

2020/12/26 17:58:07 https://<TESTSA>.blob.core.windows.net/<TESTCONTAINER>/readme.txt
2020/12/26 17:58:07 write error: -> github.com/Azure/azure-storage-blob-go/azblob.newStorageError, /home/<MYUSER>/go/pkg/mod/github.com/!azure/<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="20415a5552450d53544f524147450d424c4f420d474f6056100e11120e10" rel="noreferrer noopener nofollow">[email protected]</a>/azblob/zc_storage_error.go:42
===== RESPONSE ERROR (ServiceCode=) =====
Description=Authentication information is not given in the correct format. Check the value of Authorization header.
RequestId:f30c063e-901e-0046-2cb0-db4781000000
Time:2020-12-26T17:58:07.7810745Z, Details:
Code: InvalidAuthenticationInfo
PUT https://<TESTSA>.blob.core.windows.net/<TESTCONTAINER>/readme.txt?blockid=j%2BItsAdqRN6EScZ3S2r8QwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA%3D%3D&comp=block&timeout=61
Authorization: REDACTED
Content-Length: [12]
User-Agent: [Azure-Storage/0.12 (go1.13.9; linux)]
X-Ms-Client-Request-Id: [21638ec4-138c-434d-4b53-d13924e51966]
X-Ms-Version: [2019-12-12]
--------------------------------------------------------------------------------
RESPONSE Status: 400 Authentication information is not given in the correct format. Check the value of Authorization header.
Content-Length: [298]
Content-Type: [application/xml]
Date: [Sat, 26 Dec 2020 17:58:07 GMT]
Server: [Windows-Azure-Blob/1.0 Microsoft-HTTPAPI/2.0]
X-Ms-Request-Id: [f30c063e-901e-0046-2cb0-db4781000000]


exit status 1

我还使用 azcli 命令进行了验证,并且能够毫无困难地将示例 txt 文件 helloworld 上传到存储容器。我使用的命令:

az login --identity
az storage blob upload --container-name <TESTCONTAINER> --account-name <TESTSA> --name helloworld --file helloworld --auth-mode login

回应:

Finished[#############################################################]  100.0000%
{
"etag": "\"0x8D8A9CCDD921BA7\"",
"lastModified": "2020-12-26T18:34:22+00:00"
}

谢谢。

最佳答案

您引用的代码示例授权 Shared KeyPut Blob API,但不是 Azure AD。

credential, err := NewSharedKeyCredential(accountName, accountKey)

如果您想通过 ServicePrincipalToken 使用 Azure AD 进行授权,请参阅 Azure Active Directory authentication for Go .

applicationSecret := "APPLICATION_SECRET"

spt, err := adal.NewServicePrincipalToken(
*oauthConfig,
appliationID,
applicationSecret,
resource,
callbacks...)
if err != nil {
return nil, err
}

// Acquire a new access token
err = spt.Refresh()
if (err == nil) {
token := spt.Token
}

关于azure - 使用 SDK azblob 和托管服务标识将文件从 Azure VM 上传到 Azure 存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65459622/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com