Azure Storage Blob Go SDK示例

Posted taro_秋刀鱼

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Azure Storage Blob Go SDK示例相关的知识,希望对你有一定的参考价值。

简介


前面一篇博客介绍了关于Azure ManagerAPI Go SDK的使用,本篇继续介绍使用Blob Go SDK 操作中国区Azure Blob。

SDK下载:

go get github.com/Azure/azure-storage-blob-go/2016-05-31/azblob

示例程序:

package main

import (
    "bufio"
    "bytes"
    "context"
    "fmt"
    "io/ioutil"
    "log"
    "math/rand"
    "net/url"
    "os"
    "strconv"
    "time"

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

func randomString() string {
    r := rand.New(rand.NewSource(time.Now().UnixNano()))
    return strconv.Itoa(r.Int())
}

func handleErrors(err error) {
    if err != nil {
        if serr, ok := err.(azblob.StorageError); ok { // This error is a Service-specific
            switch serr.ServiceCode() { // Compare serviceCode to ServiceCodeXxx constants
            case azblob.ServiceCodeContainerAlreadyExists:
                fmt.Println("Received 409. Container already exists")
                return
            }
        }
        log.Fatal(err)
    }
}

func main() {
    fmt.Printf("Azure Blob storage quick start sample\n")

    var accountName string = "<storage account name>"
    var accountKey string = "<storage account key>"
    if len(accountName) == 0 || len(accountKey) == 0 {
        log.Fatal("Either the AZURE_STORAGE_ACCOUNT or AZURE_STORAGE_ACCESS_KEY environment variable is not set")
    }

    // Create a default request pipeline using your storage account name and account key.
    credential := azblob.NewSharedKeyCredential(accountName, accountKey)
    p := azblob.NewPipeline(credential, azblob.PipelineOptions{})

    // Create a random string for the quick start container
    containerName := fmt.Sprintf("quickstart-%s", randomString())

    // From the Azure portal, get your storage account blob service URL endpoint.
    URL, _ := url.Parse(
        fmt.Sprintf("https://%s.blob.core.chinacloudapi.cn/%s", accountName, containerName))

    // Create a ContainerURL object that wraps the container URL and a request
    // pipeline to make requests.
    containerURL := azblob.NewContainerURL(*URL, p)

    // Create the container
    fmt.Printf("Creating a container named %s\n", containerName)
    ctx := context.Background() // This example uses a never-expiring context
    _, err := containerURL.Create(ctx, azblob.Metadata{}, azblob.PublicAccessNone)
    handleErrors(err)

    // Create a file to test the upload and download.
    fmt.Printf("Creating a dummy file to test the upload and download\n")
    data := []byte("hello world\nthis is a blob\n")
    fileName := randomString()
    err = ioutil.WriteFile(fileName, data, 0700)
    handleErrors(err)

    // Here‘s how to upload a blob.
    blobURL := containerURL.NewBlockBlobURL(fileName)
    file, err := os.Open(fileName)
    handleErrors(err)

    // You can use the low-level PutBlob API to upload files. Low-level APIs are simple wrappers for the Azure Storage REST APIs.
    // Note that PutBlob can upload up to 256MB data in one shot. Details: https://docs.microsoft.com/en-us/rest/api/storageservices/put-blob
    // Following is commented out intentionally because we will instead use UploadFileToBlockBlob API to upload the blob
    // _, err = blobURL.PutBlob(ctx, file, azblob.BlobHTTPHeaders{}, azblob.Metadata{}, azblob.BlobAccessConditions{})
    // handleErrors(err)

    // The high-level API UploadFileToBlockBlob function uploads blocks in parallel for optimal performance, and can handle large files as well.
    // This function calls PutBlock/PutBlockList for files larger 256 MBs, and calls PutBlob for any file smaller
    fmt.Printf("Uploading the file with blob name: %s\n", fileName)
    _, err = azblob.UploadFileToBlockBlob(ctx, file, blobURL, azblob.UploadToBlockBlobOptions{
        BlockSize:   4 * 1024 * 1024,
        Parallelism: 16})
    handleErrors(err)

    // List the blobs in the container
    for marker := (azblob.Marker{}); marker.NotDone(); {
        // Get a result segment starting with the blob indicated by the current Marker.
        listBlob, err := containerURL.ListBlobs(ctx, marker, azblob.ListBlobsOptions{})
        handleErrors(err)

        // ListBlobs returns the start of the next segment; you MUST use this to get
        // the next segment (after processing the current result segment).
        marker = listBlob.NextMarker

        // Process the blobs returned in this result segment (if the segment is empty, the loop body won‘t execute)
        for _, blobInfo := range listBlob.Blobs.Blob {
            fmt.Print("Blob name: " + blobInfo.Name + "\n")
        }
    }

    // Here‘s how to download the blob. NOTE: This method automatically retries if the connection fails
    // during download (the low-level GetBlob function does NOT retry errors when reading from its stream).
    stream := azblob.NewDownloadStream(ctx, blobURL.GetBlob, azblob.DownloadStreamOptions{})
    downloadedData := &bytes.Buffer{}
    _, err = downloadedData.ReadFrom(stream)
    handleErrors(err)

    // The downloaded blob data is in downloadData‘s buffer. :Let‘s print it
    fmt.Printf("Downloaded the blob: " + downloadedData.String())

    // Cleaning up the quick start by deleting the container and the file created locally
    fmt.Printf("Press enter key to delete the sample files, example container, and exit the application.\n")
    bufio.NewReader(os.Stdin).ReadBytes(‘\n‘)
    fmt.Printf("Cleaning up.\n")
    containerURL.Delete(ctx, azblob.ContainerAccessConditions{})
    file.Close()
    os.Remove(fileName)
}

测试结果:

Azure Blob storage quick start sample
Creating a container named quickstart-6677502160360014613
Creating a dummy file to test the upload and download
Uploading the file with blob name: 186632235901289029
Blob name: 186632235901289029
Downloaded the blob: hello world
this is a blob
Press enter key to delete the sample files, example container, and exit the application.

Cleaning up.

参考链接:

storage-blobs-go-quickstart

以上是关于Azure Storage Blob Go SDK示例的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 .NET v12 SDK 在 Azure Blob 存储中上传具有指定 ContentType 的 Blob?

如何从适用于 Node.js 的 Azure blob v12 SDK 中删除 blob

使用 .NET SDK 获取 Azure Blob 存储区域?

Azure Blob PHP SDK - 直接从自定义多部分 API 请求上传到 Azure 存储

使用 azure-storage-blob 或 azure-storage 上传和删除 Azure 存储 Blob

Azure.Cosmos 还是 Microsoft.Azure.Cosmos、Azure.Storage.Blob 还是 Microsoft.Azure.Storage.Blob?适用于 .NET C