Muokkaa

Jaa


Get started with Azure Blob Storage and Go

This article shows you how to connect to Azure Blob Storage by using the Azure Blob Storage client module for Go. Once connected, your code can operate on containers, blobs, and features of the Blob Storage service.

API reference documentation | Library source code | Package (pkg.go.dev)

Prerequisites

Set up your project

This section walks you through preparing a project to work with the Azure Blob Storage client module for Go.

From your GOPATH, install the azblob module using the following command:

go get github.com/Azure/azure-sdk-for-go/sdk/storage/azblob

To authenticate with Microsoft Entra ID (recommended), install the azidentity module using the following command:

go get github.com/Azure/azure-sdk-for-go/sdk/azidentity

Then open your code file and add the necessary import paths. In this example, we add the following to our .go file:

import (
    "github.com/Azure/azure-sdk-for-go/sdk/storage/azblob"
    "github.com/Azure/azure-sdk-for-go/sdk/azidentity"
)

Blob client module information:

  • azblob: Contains the methods that you can use to operate on the service, containers, and blobs.

Authorize access and connect to Blob Storage

To connect an application to Blob Storage, create a client object using azblob.NewClient. This object is your starting point to interact with data resources at the storage account level. You can use it to operate on the storage account and its containers.

To learn more about creating and managing client objects, including best practices, see Create and manage client objects that interact with data resources.

You can authorize a client object using a Microsoft Entra authorization token (recommended), an account access key, or a shared access signature (SAS).

To authorize with Microsoft Entra ID, you need to use a security principal. The following articles provide guidance on different authentication scenarios:

Authorize access using DefaultAzureCredential

An easy and secure way to authorize access and connect to Blob Storage is to obtain an OAuth token by creating a DefaultAzureCredential instance. You can then use that credential to create the client object using azblob.NewClient.

func getServiceClientTokenCredential(accountURL string) *azblob.Client {
    // Create a new service client with token credential
    credential, err := azidentity.NewDefaultAzureCredential(nil)
    handleError(err)

    client, err := azblob.NewClient(accountURL, credential, nil)
    handleError(err)

    return client
}

Build your application

As you build applications to work with data resources in Azure Blob Storage, your code primarily interacts with three resource types: storage accounts, containers, and blobs. To learn more about these resource types, how they relate to one another, and how apps interact with resources, see Understand how apps interact with Blob Storage data resources.

The following guides show you how to work with data resources and perform specific actions using the Azure Blob Storage client module for Go:

Guide Description
Create a container Create containers.
Delete and restore containers Delete containers, and if soft-delete is enabled, restore deleted containers.
List containers List containers in an account and the various options available to customize a listing.
Manage properties and metadata (containers) Manage container properties and metadata.
Upload blobs Learn how to upload blobs by using strings, streams, file paths, and other methods.
Download blobs Download blobs by using strings, streams, and file paths.
List blobs List blobs in different ways.
Delete and restore blobs Delete blobs, and if soft-delete is enabled, restore deleted blobs.
Manage properties and metadata (blobs) Manage container properties and metadata.

Note

The code samples in this guide are intended to help you get started with Azure Blob Storage and Go. You should modify error handling and Context values to meet the needs of your application.