Copy a blob from a source object URL with Go
This article shows how to copy a blob from a source object URL using the Azure Storage client module for Go. You can copy a blob from a source within the same storage account, from a source in a different storage account, or from any accessible object retrieved via HTTP GET request on a given URL.
The client library methods covered in this article use the Put Blob From URL and Put Block From URL REST API operations. These methods are preferred for copy scenarios where you want to move data into a storage account and have a URL for the source object. For copy operations where you want asynchronous scheduling, see Copy a blob with asynchronous scheduling using Go.
Prerequisites
- Azure subscription - create one for trial
- Azure storage account - create a storage account
- Go 1.18+
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"
)
These import paths represent the minimum needed to get started. 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 perform a copy operation. 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 From URL or Put Block From URL.
About copying blobs from a source object URL
The Put Blob From URL
operation creates a new block blob where the contents of the blob are read from a given URL. The operation completes synchronously.
The source can be any object retrievable via a standard HTTP GET request on the given URL. This includes block blobs, append blobs, page blobs, blob snapshots, blob versions, or any accessible object inside or outside Azure.
When the source object is a block blob, all committed blob content is copied. The content of the destination blob is identical to the content of the source, but the list of committed blocks isn't preserved and uncommitted blocks aren't copied.
The destination is always a block blob, either an existing block blob, or a new block blob created by the operation. The contents of an existing blob are overwritten with the contents of the new blob.
The Put Blob From URL
operation always copies the entire source blob. Copying a range of bytes or set of blocks isn't supported. To perform partial updates to a block blob's contents by using a source URL, use the Put Block From URL API along with Put Block List
.
To learn more about the Put Blob From URL
operation, including blob size limitations and billing considerations, see Put Blob From URL remarks.
Copy a blob from a source object URL
This section gives an overview of methods provided by the Azure Storage client library for Go to perform a copy operation from a source object URL.
The following method wraps the Put Blob From URL REST API operation, and creates a new block blob where the contents of the blob are read from a given URL:
This method is preferred for scenarios where you want to move data into a storage account and have a URL for the source object.
For large objects, you might choose to work with individual blocks. The following method wraps the Put Block From URL REST API operation. This method creates a new block to be committed as part of a blob where the contents are read from a source URL:
Copy a blob from a source within Azure
If you're copying a blob from a source within Azure, access to the source blob can be authorized via Microsoft Entra ID (recommended), a shared access signature (SAS), or an account key.
The following code example shows a scenario for copying a source blob within Azure. In this example, we also set the access tier for the destination blob to Cool
using the UploadBlobFromURLOptions struct.
func copyFromSourceURL(srcBlob *blockblob.Client, destBlob *blockblob.Client) {
// Set copy options
copyOptions := blockblob.UploadBlobFromURLOptions{
Tier: to.Ptr(blob.AccessTierCool),
}
// Copy the blob from the source URL to the destination blob
_, err := destBlob.UploadBlobFromURL(context.TODO(), srcBlob.URL(), ©Options)
handleError(err)
}
The following example shows sample usage:
// TODO: replace <storage-account-name> placeholders with actual storage account names
srcURL := "https://<src-storage-account-name>.blob.core.chinacloudapi.cn/"
destURL := "https://<dest-storage-account-name>.blob.core.chinacloudapi.cn/"
credential, err := azidentity.NewDefaultAzureCredential(nil)
handleError(err)
srcClient, err := azblob.NewClient(srcURL, credential, nil)
handleError(err)
destClient, err := azblob.NewClient(destURL, credential, nil)
handleError(err)
srcBlob := srcClient.ServiceClient().NewContainerClient("source-container").NewBlockBlobClient("source-blob")
destBlob := destClient.ServiceClient().NewContainerClient("destination-container").NewBlockBlobClient("destination-blob-1")
copyFromSourceURL(srcBlob, destBlob)
Copy a blob from a source outside of Azure
You can perform a copy operation on any source object that can be retrieved via HTTP GET request on a given URL, including accessible objects outside of Azure. The following code example shows a scenario for copying a blob from an accessible source object URL.
func copyFromExternalSource(srcURL string, destBlob *blockblob.Client) {
// Set copy options
copyOptions := blockblob.UploadBlobFromURLOptions{
Tier: to.Ptr(blob.AccessTierCool),
}
// Copy the blob from the source URL to the destination blob
_, err := destBlob.UploadBlobFromURL(context.TODO(), srcURL, ©Options)
handleError(err)
}
The following example shows sample usage:
externalURL := "<source-url>"
destBlob = destClient.ServiceClient().NewContainerClient("destination-container").NewBlockBlobClient("destination-blob-2")
copyFromExternalSource(externalURL, destBlob)
Resources
To learn more about copying blobs using the Azure Blob Storage client library for Go, see the following resources.
Code samples
- View code samples from this article (GitHub)
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 covered in this article use the following REST API operations:
- Put Blob From URL (REST API)
- Put Block From URL (REST API)
Client module resources
Related content
- This article is part of the Blob Storage developer guide for Go. To learn more, see the full list of developer guide articles at Build your Go app.