azblob.ContainerClient 在 azblob sdk for go 迁移到 v1 后取消定义

问题描述 投票:0回答:1
enter func NewFlowlogParse(filename string, container *azblob.ContainerClient, chmsg chan string, bookmark chan Bookmark, bk_time string) 

containerURL、azblob.UploadToBlockBlobOptions 也遇到同样的问题

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

// Branch off of the container into a block blob client
blockBlob := containerURL.NewBlockBlobURL("data/flowlog_bookmark.json")
// Upload data to the block blob
_, err = blockBlob.Upload(ctx, bytes.NewReader(data), azblob.UploadToBlockBlobOptions{})
if err != nil {
    log.Println(err)
}

wg.Done()

containerURL、azblob.UploadToBlockBlobOptions 也遇到同样的问题

当我尝试将 azblolb-sdk-blob-for-go 迁移到 v.1.x 时,我遇到错误

https://github.com/Azure/azure-sdk-for-go

迁移到 azblob v1(azure sdk for go)时收到错误,我们面临以下错误 pkg/parser/flowlog.go:73:57: undefined: azblob.ContainerClient

pkg/parser/flowlog.go:189:60:未定义:azblob.ContainerURL 甚至认为它们存在于 go 包的 sdk 中

azure go azure-blob-storage
1个回答
0
投票
  • 下面的代码是一个 Go 程序,它与 Azure Blob 存储交互,创建容器,上传 Blob,下载 Blob,并使用 NewContainerURL 列出容器中的 Blob。

  • 根据文档

    azure-storage-blob-go
    的最新版本是0.15.0。

  • 要从 GitHub 获取最新版本的 Azure Blob Storage Go SDK,请将其安装到您的 Go 环境中。

go get -u github.com/Azure/azure-storage-blob-go/azblob
package main

import (
    "context"
    "fmt"
    "io"
    "log"
    "net/url"
    "strings"

    "github.com/Azure/azure-storage-blob-go/azblob"
)

func main() {
    
    accountName, accountKey := accountInfo()

    
    credential, err := azblob.NewSharedKeyCredential(accountName, accountKey)
    if err != nil {
        log.Fatal(err)
    }

    
    u, _ := url.Parse(fmt.Sprintf("https://%s.blob.core.windows.net", accountName))

    
    serviceURL := azblob.NewServiceURL(*u, azblob.NewPipeline(credential, azblob.PipelineOptions{}))

    
    ctx := context.Background() 

    
    containerURL := serviceURL.NewContainerURL("sampathpujari1") 

    
    _, err = containerURL.Create(ctx, azblob.Metadata{}, azblob.PublicAccessNone)
    if err != nil {
        log.Fatal(err)
    }

    
    blobURL := containerURL.NewBlockBlobURL("HelloWorld.txt")

    
    data := "Hello World!"
    _, err = blobURL.Upload(ctx, strings.NewReader(data), azblob.BlobHTTPHeaders{ContentType: "text/plain"}, azblob.Metadata{}, azblob.BlobAccessConditions{}, azblob.DefaultAccessTier, nil, azblob.ClientProvidedKeyOptions{}, azblob.ImmutabilityPolicyOptions{})
    if err != nil {
        log.Fatal(err)
    }


    get, err := blobURL.Download(ctx, 0, azblob.CountToEnd, azblob.BlobAccessConditions{}, false, azblob.ClientProvidedKeyOptions{})
    if err != nil {
        log.Fatal(err)
    }

    var downloadedData strings.Builder
    _, err = io.Copy(&downloadedData, get.Body(azblob.RetryReaderOptions{}))
    if err != nil {
        log.Fatal(err)
    }

    if data != downloadedData.String() {
        log.Fatal("downloaded data doesn't match uploaded data")
    }

    
    for marker := (azblob.Marker{}); marker.NotDone(); { 
        
        listBlob, err := containerURL.ListBlobsFlatSegment(ctx, marker, azblob.ListBlobsSegmentOptions{})
        if err != nil {
            log.Fatal(err)
        }
    
        marker = listBlob.NextMarker

        
        for _, blobInfo := range listBlob.Segment.BlobItems {
            fmt.Println("Blob name: " + blobInfo.Name)
        }
    }

    // Delete the blob we created earlier.
    //_, err = blobURL.Delete(ctx, azblob.DeleteSnapshotsOptionNone, azblob.BlobAccessConditions{})
    //if err != nil {
    //log.Fatal(err)
    //}

    // Delete the container we created earlier.
    //_, err = containerURL.Delete(ctx, azblob.ContainerAccessConditions{})
    //if err != nil {
    //log.Fatal(err)
    //}
}

func accountInfo() (string, string) {
    // Replace with your actual storage account name and key
    return "your-storage-account-name", "your-storage-account-key"
}


输出: enter image description here

蔚蓝: enter image description here

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.