Upload a block blob with Go

This article shows how to upload a blob using the Azure Storage client module for Go. You can upload data to a block blob from a file path, a stream, a binary object, or a text string. You can also upload blobs with index tags.

Prerequisites

Set up your environment

If you don't have an existing project, this section shows how to set up a project to work with the Azure Blob Storage client module for Go. The steps include module installation, adding import paths, and creating an authorized client object. For details, see Get started with Azure Blob Storage and Go.

Install modules

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

Add import paths

In your code file, add the following import paths:

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

Some code examples in this article might require additional import paths. For specific details and example usage, see Code samples.

Create a client object

To connect an app to Blob Storage, create a client object using azblob.NewClient. The following example shows how to create a client object using DefaultAzureCredential for authorization:

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
}

Authorization

The authorization mechanism must have the necessary permissions to upload a blob. For authorization with Microsoft Entra ID (recommended), you need Azure RBAC built-in role Storage Blob Data Contributor or higher. To learn more, see the authorization guidance for Put Blob (REST API) and Put Block (REST API).

Upload data to a block blob

To upload a blob, call any of the following methods from the client object:

To perform the upload, the client library might use either Put Blob or a series of Put Block calls followed by Put Block List. This behavior depends on the overall size of the object and how the data transfer options are set.

Upload a block blob from a local file path

The following example uploads a local file to a block blob:

func uploadBlobFile(client *azblob.Client, containerName string, blobName string) {
    // Open the file for reading
    file, err := os.OpenFile("path/to/sample/file", os.O_RDONLY, 0)
    handleError(err)

    defer file.Close()

    // Upload the file to the specified container with the specified blob name
    _, err = client.UploadFile(context.TODO(), containerName, blobName, file, nil)
    handleError(err)
}

Upload a block blob from a stream

The following example creates a Reader instance and reads from a string as if it were a stream of bytes. The stream is then uploaded to a block blob:

func uploadBlobStream(client *azblob.Client, containerName string, blobName string) {
    data := "Hello, world!"
    blobContentReader := strings.NewReader(data)

    // Upload the file to the specified container with the specified blob name
    _, err := client.UploadStream(context.TODO(), containerName, blobName, blobContentReader, nil)
    handleError(err)
}

Upload binary data to a block blob

The following example uploads binary data to a block blob:

func uploadBlobBuffer(client *azblob.Client, containerName string, blobName string) {
    // Create a buffer with the content of the file to upload
    data := []byte("Hello, world!")

    // Upload the data to a block blob
    _, err := client.UploadBuffer(context.TODO(), containerName, blobName, data, nil)
    handleError(err)
}

Upload a block blob with index tags

The following example uploads a block blob with index tags:

func uploadBlobWithIndexTags(client *azblob.Client, containerName string, blobName string) {
    // Create a buffer with the content of the file to upload
    data := []byte("Hello, world!")

    // Upload the data to a block blob with index tags
    _, err := client.UploadBuffer(context.TODO(), containerName, blobName, data, &azblob.UploadBufferOptions{
        Tags: map[string]string{
            "key1": "value1",
            "key2": "value2",
        },
    })
    handleError(err)
}

Upload a block blob with configuration options

You can define client library configuration options when uploading a blob. These options can be tuned to improve performance, enhance reliability, and optimize costs. The following code examples show how to define configuration options for an upload operation.

Specify data transfer options for upload

You can set configuration options when uploading a blob to optimize performance. The following configuration options are available for upload operations:

  • BlockSize: The size of each block when uploading a block blob. The default value is 1 MiB.
  • Concurrency: The maximum number of parallel connections to use during upload. The default value is 1.

For more information on transfer size limits for Blob Storage, see Scale targets for Blob storage.

The following code example shows how to specify data transfer options using the UploadFileOptions. The values provided in this sample aren't intended to be a recommendation. To properly tune these values, you need to consider the specific needs of your app.

func uploadBlobWithTransferOptions(client *azblob.Client, containerName string, blobName string) {
    // Open the file for reading
    file, err := os.OpenFile("path/to/sample/file", os.O_RDONLY, 0)
    handleError(err)

    defer file.Close()

    // Upload the data to a block blob with transfer options
    _, err = client.UploadFile(context.TODO(), containerName, blobName, file,
        &azblob.UploadFileOptions{
            BlockSize:   int64(4 * 1024 * 1024), // 4 MiB
            Concurrency: uint16(2),
        })
    handleError(err)
}

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.

Resources

To learn more about uploading blobs using the Azure Blob Storage client module for Go, see the following resources.

Code samples

REST API operations

The Azure SDK for Go contains libraries that build on top of the Azure REST API, allowing you to interact with REST API operations through familiar Go paradigms. The client library methods for uploading blobs use the following REST API operations:

Client module resources

See also