登录
首页 >  Golang >  Go问答

如何在Golang中为Azure SDK指定x509证书

来源:stackoverflow

时间:2024-04-04 21:00:38 167浏览 收藏

你在学习Golang相关的知识吗?本文《如何在Golang中为Azure SDK指定x509证书》,主要介绍的内容就涉及到,如果你想提升自己的开发能力,就不要错过这篇文章,大家要知道编程理论基础和实战操作都是不可或缺的哦!

问题内容

我正在尝试连接以使用 azure sdk for golang 将文件从在线容器下载到我的设备,并使用 azure 提供的连接字符串进行连接。对于上下文来说,这是在嵌入式 linux 版本上运行的

我有两个问题,首先如何将特定证书传递给azure sdk以用于连接,因为目前当我连接时,我遇到了这个问题

get "https://transaction.blob.core.windows.net/transactions?comp=list&restype=container": x509: 由未知权威机构签名的证书

或者如果失败,我如何生成正确的证书并将其放入 /etc/ssl 中?据我所知,我认为这就是 go 寻找证书的地方。

还有第二个问题,如果我的文件夹结构类似于 /transaction/my-libs/images/1.0.0/libimage.bin(其中事务是我的),我应该使用 azure sdk for go 的什么函数从在线 blob 下载blob 容器。

func testConnection(){
    Println("TESTING CONNECTION")

    connStr := "..." // actual connection string hidden 

    serviceClient, err := azblob.NewServiceClientFromConnectionString(connStr, nil)

    // crashes here <------------

    //ctx := context.Background()
    //container := serviceClient.NewContainerClient("transactions")
    //
    //_, err = container.Create(ctx, nil)
    //
    //blockBlob := container.NewBlockBlobClient("erebor-libraries")
    //_, err = blockBlob.Download(ctx, nil)


    //Open a buffer, reader, and then download!
    downloadedData := &bytes.Buffer{}
    reader := get.Body(RetryReaderOptions{}) // RetryReaderOptions has a lot of in-depth tuning abilities, but for the sake of simplicity, we'll omit those here.
    _, err = downloadedData.ReadFrom(reader)
    err = reader.Close()
    if data != downloadedData.String() {
        err := errors.New("downloaded data doesn't match uploaded data")
        if err != nil {
            return
        }
    }

    pager := container.ListBlobsFlat(nil)
    for pager.NextPage(ctx) {
        resp := pager.PageResponse()

        for _, v := range resp.ContainerListBlobFlatSegmentResult.Segment.BlobItems {
            fmt.Println(*v.Name)
        
    }

}

正确答案


• 您可以使用以下 azure sdk for go 命令将特定证书传递到 azure sdk,以通过为其创建服务主体来连接到其他 azure 资源:-

‘ type ClientCertificateConfig struct {
  ClientID            string
  CertificatePath     string
  CertificatePassword string
  TenantID            string
  AuxTenants          []string
  AADEndpoint         string
  Resource            string
 } ‘

有关客户端证书的创建及其使用的更多信息,请参阅下面的文档链接了解更多详细信息:- https://pkg.go.dev/github.com/Azure/go-autorest/autorest/azure/auth#ClientCertificateConfig

此外,即使您的文件夹结构是'/transaction/my-libs/images/1.0.0/libimage.bin',但 blob url 与 blob 中提到的文件夹层次结构是唯一的url,因此,当连接到 azure 存储帐户以下载 blob 时,请以单引号表示法提及 url,以便明确 blob 路径。

请参阅下面的示例代码,通过 azure sdk for go 下载 blob:-

https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/storage/azblob#example-package

https://pkg.go.dev/github.com/Azure/azure-storage-blob-go/azblob#pkg-examples

本篇关于《如何在Golang中为Azure SDK指定x509证书》的介绍就到此结束啦,但是学无止境,想要了解学习更多关于Golang的相关知识,请关注golang学习网公众号!

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