Skip to content

Commit b957ba5

Browse files
authored
Merge pull request #281347 from pauljewellmsft/copy-go
[Blob dev guide] Add copy operation guidance - Go
2 parents d521e59 + d3f5223 commit b957ba5

8 files changed

+278
-4
lines changed

articles/storage/blobs/TOC.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -985,6 +985,14 @@ items:
985985
href: storage-blob-upload-go.md
986986
- name: Download blobs
987987
href: storage-blob-download-go.md
988+
- name: Copy blobs
989+
items:
990+
- name: Overview of copy operations
991+
href: storage-blob-copy-go.md
992+
- name: Copy from a source object URL
993+
href: storage-blob-copy-url-go.md
994+
- name: Copy with asynchronous scheduling
995+
href: storage-blob-copy-async-go.md
988996
- name: List blobs
989997
href: storage-blobs-list-go.md
990998
- name: Delete and restore blobs
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
---
2+
title: Copy a blob with asynchronous scheduling using Go
3+
titleSuffix: Azure Storage
4+
description: Learn how to copy a blob with asynchronous scheduling in Azure Storage by using the Go client module.
5+
author: pauljewellmsft
6+
7+
ms.author: pauljewell
8+
ms.date: 07/25/2024
9+
ms.service: azure-blob-storage
10+
ms.topic: how-to
11+
ms.devlang: golang
12+
ms.custom: devx-track-go, devguide-go
13+
---
14+
15+
# Copy a blob with asynchronous scheduling using Go
16+
17+
[!INCLUDE [storage-dev-guide-selector-copy-async](../../../includes/storage-dev-guides/storage-dev-guide-selector-copy-async.md)]
18+
19+
This article shows how to copy a blob with asynchronous scheduling using the [Azure Storage client module for Go](https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/storage/azblob#section-readme). 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. You can also abort a pending copy operation.
20+
21+
The methods covered in this article use the [Copy Blob](/rest/api/storageservices/copy-blob) REST API operation, and can be used when you want to perform a copy with asynchronous scheduling. For most copy scenarios where you want to move data into a storage account and have a URL for the source object, see [Copy a blob from a source object URL with Go](storage-blob-copy-url-go.md).
22+
23+
[!INCLUDE [storage-dev-guide-prereqs-go](../../../includes/storage-dev-guides/storage-dev-guide-prereqs-go.md)]
24+
25+
## Set up your environment
26+
27+
[!INCLUDE [storage-dev-guide-project-setup-go](../../../includes/storage-dev-guides/storage-dev-guide-project-setup-go.md)]
28+
29+
#### Authorization
30+
31+
The authorization mechanism must have the necessary permissions to perform a copy operation, or to abort a pending copy. 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 [Copy Blob](/rest/api/storageservices/copy-blob#authorization) or [Abort Copy Blob](/rest/api/storageservices/abort-copy-blob#authorization).
32+
33+
## About copying blobs with asynchronous scheduling
34+
35+
The `Copy Blob` operation can finish asynchronously and is performed on a best-effort basis, which means that the operation isn't guaranteed to start immediately or complete within a specified time frame. The copy operation is scheduled in the background and performed as the server has available resources. The operation can complete synchronously if the copy occurs within the same storage account.
36+
37+
A `Copy Blob` operation can perform any of the following actions:
38+
39+
- Copy a source blob to a destination blob with a different name. The destination blob can be an existing blob of the same blob type (block, append, or page), or it can be a new blob created by the copy operation.
40+
- Copy a source blob to a destination blob with the same name, which replaces the destination blob. This type of copy operation removes any uncommitted blocks and overwrites the destination blob's metadata.
41+
- Copy a source file in the Azure File service to a destination blob. The destination blob can be an existing block blob, or can be a new block blob created by the copy operation. Copying from files to page blobs or append blobs isn't supported.
42+
- Copy a snapshot over its base blob. By promoting a snapshot to the position of the base blob, you can restore an earlier version of a blob.
43+
- Copy a snapshot to a destination blob with a different name. The resulting destination blob is a writeable blob and not a snapshot.
44+
45+
The source blob for a copy operation can be one of the following types: block blob, append blob, page blob, blob snapshot, or blob version. The copy operation always copies the entire source blob or file. Copying a range of bytes or set of blocks isn't supported.
46+
47+
If the destination blob already exists, it must be of the same blob type as the source blob, and the existing destination blob is overwritten. The destination blob can't be modified while a copy operation is in progress, and a destination blob can only have one outstanding copy operation.
48+
49+
To learn more about the `Copy Blob` operation, including information about properties, index tags, metadata, and billing, see [Copy Blob remarks](/rest/api/storageservices/copy-blob#remarks).
50+
51+
## Copy a blob with asynchronous scheduling
52+
53+
This section gives an overview of methods provided by the Azure Storage client module for Go to perform a copy operation with asynchronous scheduling.
54+
55+
The following methods wrap the [Copy Blob](/rest/api/storageservices/copy-blob) REST API operation, and begin an asynchronous copy of data from the source blob:
56+
57+
- [StartCopyFromURL](https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/blob#Client.StartCopyFromURL)
58+
59+
## Copy a blob from a source within Azure
60+
61+
If you're copying a blob within the same storage account, the operation can complete synchronously. Access to the source blob can be authorized via Microsoft Entra ID (recommended), a shared access signature (SAS), or an account key. For an alterative synchronous copy operation, see [Copy a blob from a source object URL with Go](storage-blob-copy-url-go.md).
62+
63+
If the copy source is a blob in a different storage account, the operation can complete asynchronously. The source blob must either be public or authorized via SAS token. The SAS token needs to include the **Read ('r')** permission. To learn more about SAS tokens, see [Delegate access with shared access signatures](../common/storage-sas-overview.md).
64+
65+
The following example shows a scenario for copying a source blob from a different storage account with asynchronous scheduling. In this example, we create a source blob URL with an appended user delegation SAS token. The example assumes you provide your own SAS. The example also shows how to lease the source blob during the copy operation to prevent changes to the blob from a different client. The `Copy Blob` operation saves the `ETag` value of the source blob when the copy operation starts. If the `ETag` value is changed before the copy operation finishes, the operation fails. We also set the access tier for the destination blob to `Cool` using the [StartCopyFromURLOptions](https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/blob#StartCopyFromURLOptions) struct.
66+
67+
:::code language="go" source="~/blob-devguide-go/cmd/copy-blob-async/copy_blob_async.go" id="snippet_copy_from_source_async":::
68+
69+
The following example shows sample usage:
70+
71+
:::code language="go" source="~/blob-devguide-go/cmd/copy-blob-async/copy_blob_async.go" id="snippet_copy_from_source_async_usage":::
72+
73+
> [!NOTE]
74+
> User delegation SAS tokens offer greater security, as they're signed with Microsoft Entra credentials instead of an account key. To create a user delegation SAS token, the Microsoft Entra security principal needs appropriate permissions. For authorization requirements, see [Get User Delegation Key](/rest/api/storageservices/get-user-delegation-key#authorization).
75+
76+
## Copy a blob from a source outside of Azure
77+
78+
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 example shows a scenario for copying a blob from an accessible source object URL:
79+
80+
:::code language="go" source="~/blob-devguide-go/cmd/copy-blob-async/copy_blob_async.go" id="snippet_copy_from_external_source_async":::
81+
82+
The following example shows sample usage:
83+
84+
:::code language="go" source="~/blob-devguide-go/cmd/copy-blob-async/copy_blob_async.go" id="snippet_copy_from_external_source_async_usage":::
85+
86+
## Check the status of a copy operation
87+
88+
To check the status of an asynchronous `Copy Blob` operation, you can poll the [GetProperties](https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/blob#Client.GetProperties) method and check the copy status.
89+
90+
The following code example shows how to check the status of a copy operation:
91+
92+
:::code language="go" source="~/blob-devguide-go/cmd/copy-blob-async/copy_blob_async.go" id="snippet_check_copy_status":::
93+
94+
## Abort a copy operation
95+
96+
Aborting a pending `Copy Blob` operation results in a destination blob of zero length. However, the metadata for the destination blob has the new values copied from the source blob or set explicitly during the copy operation. To keep the original metadata from before the copy, make a snapshot of the destination blob before calling one of the copy methods.
97+
98+
To abort a pending copy operation, call the following operation:
99+
100+
- [AbortCopyFromURL](https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/blockblob#Client.AbortCopyFromURL)
101+
102+
This method wraps the [Abort Copy Blob](/rest/api/storageservices/abort-copy-blob) REST API operation, which cancels a pending `Copy Blob` operation. The following code example shows how to abort a pending `Copy Blob` operation:
103+
104+
:::code language="go" source="~/blob-devguide-go/cmd/copy-blob-async/copy_blob_async.go" id="snippet_abort_copy":::
105+
106+
## Resources
107+
108+
To learn more about copying blobs with asynchronous scheduling using the Azure Blob Storage client module for Go, see the following resources.
109+
110+
### Code samples
111+
112+
- [View code samples from this article (GitHub)](https://github.com/Azure-Samples/blob-storage-devguide-go/blob/main/cmd/copy-blob-async/copy_blob_async.go)
113+
114+
### REST API operations
115+
116+
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 methods covered in this article use the following REST API operations:
117+
118+
- [Copy Blob](/rest/api/storageservices/copy-blob) (REST API)
119+
- [Abort Copy Blob](/rest/api/storageservices/abort-copy-blob) (REST API)
120+
121+
[!INCLUDE [storage-dev-guide-resources-go](../../../includes/storage-dev-guides/storage-dev-guide-resources-go.md)]
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
---
2+
title: Copy a blob with Go
3+
titleSuffix: Azure Storage
4+
description: Learn how to copy a blob in Azure Storage by using the Go client library.
5+
services: storage
6+
author: pauljewellmsft
7+
8+
ms.author: pauljewell
9+
ms.date: 07/25/2024
10+
ms.service: azure-blob-storage
11+
ms.topic: how-to
12+
ms.devlang: golang
13+
ms.custom: devx-track-go, devguide-go
14+
---
15+
16+
# Copy a blob with Go
17+
18+
[!INCLUDE [storage-dev-guide-selector-copy](../../../includes/storage-dev-guides/storage-dev-guide-selector-copy.md)]
19+
20+
This article provides an overview of copy operations using the [Azure Storage client module for Go](https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/storage/azblob#section-readme).
21+
22+
## About copy operations
23+
24+
Copy operations can be used to move data within a storage account, between storage accounts, or into a storage account from a source outside of Azure. When using the Blob Storage client libraries to copy data resources, it's important to understand the REST API operations behind the client library methods. The following table lists REST API operations that can be used to copy data resources to a storage account. The table also includes links to detailed guidance about how to perform these operations using the [Azure Storage client module for Go](https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/storage/azblob#section-readme).
25+
26+
| REST API operation | When to use | Client library methods | Guidance |
27+
| --- | --- | --- | --- |
28+
| [Put Blob From URL](/rest/api/storageservices/put-blob-from-url) | This operation is preferred for scenarios where you want to move data into a storage account and have a URL for the source object. This operation completes synchronously. | [UploadBlobFromURL](https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/blockblob#Client.UploadBlobFromURL) | [Copy a blob from a source object URL with Go](storage-blob-copy-url-go.md) |
29+
| [Put Block From URL](/rest/api/storageservices/put-block-from-url) | For large objects, you can use [Put Block From URL](/rest/api/storageservices/put-block-from-url) to write individual blocks to Blob Storage, and then call [Put Block List](/rest/api/storageservices/put-block-list) to commit those blocks to a block blob. This operation completes synchronously. | [StageBlockFromURL](https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/blockblob#Client.StageBlockFromURL) | [Copy a blob from a source object URL with Go](storage-blob-copy-url-go.md) |
30+
| [Copy Blob](/rest/api/storageservices/copy-blob) | This operation can be used when you want asynchronous scheduling for a copy operation. | [StartCopyFromURL](https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/blob#Client.StartCopyFromURL) | [Copy a blob with asynchronous scheduling using Go](storage-blob-copy-async-go.md) |
31+
32+
For append blobs, you can use the [Append Block From URL](/rest/api/storageservices/append-block-from-url) operation to commit a new block of data to the end of an existing append blob. The following client library method wraps this operation:
33+
34+
- [AppendBlockFromURL](https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/appendblob#Client.AppendBlockFromURL)
35+
36+
For page blobs, you can use the [Put Page From URL](/rest/api/storageservices/put-page-from-url) operation to write a range of pages to a page blob where the contents are read from a URL. The following client library method wraps this operation:
37+
38+
- [UploadPagesFromURL](https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/pageblob#Client.UploadPagesFromURL)
39+
40+
## Client library resources
41+
42+
- [Client module reference documentation](https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/storage/azblob#section-readme)
43+
- [Client module source code](https://github.com/Azure/azure-sdk-for-go/tree/main/sdk/storage/azblob)
44+
- [Package (pkg.go.dev)](https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/storage/azblob)

0 commit comments

Comments
 (0)