|
| 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)] |
0 commit comments