title | titleSuffix | description | services | author | ms.author | ms.date | ms.service | ms.topic | ms.devlang | ms.custom |
---|---|---|---|---|---|---|---|---|---|---|
Upload a blob with Go |
Azure Storage |
Learn how to upload a blob to your Azure Storage account using the Go client library. |
storage |
pauljewellmsft |
pauljewell |
05/22/2024 |
azure-blob-storage |
how-to |
golang |
devx-track-go, devguide-go |
[!INCLUDE storage-dev-guide-selector-upload]
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.
[!INCLUDE storage-dev-guide-prereqs-go]
[!INCLUDE storage-dev-guide-project-setup-go]
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).
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.
The following example uploads a local file to a block blob:
:::code language="go" source="~/blob-devguide-go/cmd/upload-blob/upload_blob.go" id="snippet_upload_blob_file":::
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:
:::code language="go" source="~/blob-devguide-go/cmd/upload-blob/upload_blob.go" id="snippet_upload_blob_stream":::
The following example uploads binary data to a block blob:
:::code language="go" source="~/blob-devguide-go/cmd/upload-blob/upload_blob.go" id="snippet_upload_blob_buffer":::
The following example uploads a block blob with index tags:
:::code language="go" source="~/blob-devguide-go/cmd/upload-blob/upload_blob.go" id="snippet_upload_blob_tags":::
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.
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.
:::code language="go" source="~/blob-devguide-go/cmd/upload-blob/upload_blob.go" id="snippet_upload_blob_transfer_options":::
[!INCLUDE storage-dev-guide-code-samples-note-go]
To learn more about uploading blobs using the Azure Blob Storage client module for Go, see the following resources.
- View code samples from this article (GitHub)
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:
[!INCLUDE storage-dev-guide-resources-go]