This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can trysigning in orchanging directories.
Access to this page requires authorization. You can trychanging directories.
This article shows how to copy a blob with asynchronous scheduling using theAzure Storage client library for JavaScript. 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.
The client library methods covered in this article use theCopy 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, seeCopy a blob from a source object URL with JavaScript.
TheCopy 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.
ACopy Blob operation can perform any of the following actions:
To learn more about theCopy Blob operation, including information about properties, index tags, metadata, and billing, seeCopy Blob remarks.
This section gives an overview of methods provided by the Azure Storage client library for JavaScript to perform a copy operation with asynchronous scheduling.
The following methods wrap theCopy Blob REST API operation, and begin an asynchronous copy of data from the source blob:
ThebeginCopyFromURL method returns a long running operation poller that allows you to wait indefinitely until the copy is completed.
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, a shared access signature (SAS), or an account key. For an alternative synchronous copy operation, seeCopy a blob from a source object URL with JavaScript.
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 theRead ('r') permission. To learn more about SAS tokens, seeDelegate access with shared access signatures.
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 shows how to generate the SAS token using the client library, but you can also provide your own. The example also shows how to lease the source blob during the copy operation to prevent changes to the blob from a different client. TheCopy Blob operation saves theETag value of the source blob when the copy operation starts. If theETag value is changed before the copy operation finishes, the operation fails.
async function copyAcrossStorageAccountsAsync(sourceBlob, destinationBlob, blobServiceClient) { // Lease the source blob to prevent changes during the copy operation const sourceBlobLease = new BlobLeaseClient(sourceBlob); // Create a SAS token that's valid for 1 hour const sasToken = await generateUserDelegationSAS(sourceBlob, blobServiceClient); const sourceBlobSASURL = sourceBlob.url + "?" + sasToken; try { await sourceBlobLease.acquireLease(-1); // Start the copy operation and wait for it to complete const copyPoller = await destinationBlob.beginCopyFromURL(sourceBlobSASURL); await copyPoller.pollUntilDone(); } catch (error) { // Handle the exception } finally { // Release the lease once the copy operation completes await sourceBlobLease.releaseLease(); }}async function generateUserDelegationSAS(sourceBlob, blobServiceClient) { // Get a user delegation key for the Blob service that's valid for 1 hour, as an example const delegationKeyStart = new Date(); const delegationKeyExpiry = new Date(Date.now() + 3600000); const userDelegationKey = await blobServiceClient.getUserDelegationKey( delegationKeyStart, delegationKeyExpiry ); // Create a SAS token that's valid for 1 hour, as an example const sasTokenStart = new Date(); const sasTokenExpiry = new Date(Date.now() + 3600000); const blobName = sourceBlob.name; const containerName = sourceBlob.containerName; const sasOptions = { blobName, containerName, permissions: BlobSASPermissions.parse("r"), startsOn: sasTokenStart, expiresOn: sasTokenExpiry, protocol: SASProtocol.HttpsAndHttp }; const sasToken = generateBlobSASQueryParameters( sasOptions, userDelegationKey, blobServiceClient.accountName ).toString(); return sasToken.toString();}Note
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, seeGet User Delegation Key.
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.
async function copyFromExternalSource(sourceURL, destinationBlob) { const copyPoller = await destinationBlob.beginCopyFromURL(sourceURL); await copyPoller.pollUntilDone();}To check the status of an asynchronousCopy Blob operation, you can poll thegetProperties method and check the copy status.
The following code example shows how to check the status of a pending copy operation:
async function checkCopyStatus(destinationBlob) { const properties = await destinationBlob.getProperties(); console.log(properties.copyStatus);}Aborting a pendingCopy 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.
To abort a pending copy operation, call the following operation:
This method wraps theAbort Copy Blob REST API operation, which cancels a pendingCopy Blob operation. The following code example shows how to abort a pendingCopy Blob operation:
async function abortCopy(destinationBlob) { const properties = await destinationBlob.getProperties(); // Check the copy status and abort if pending if (properties.copyStatus === "pending") { await destinationBlob.abortCopyFromURL(properties.copyId); }}To learn more about copying blobs with asynchronous scheduling using the Azure Blob Storage client library for JavaScript, see the following resources.
The Azure SDK for JavaScript contains libraries that build on top of the Azure REST API, allowing you to interact with REST API operations through familiar JavaScript paradigms. The client library methods covered in this article use the following REST API operations:
Was this page helpful?
Need help with this topic?
Want to try using Ask Learn to clarify or guide you through this topic?
Was this page helpful?
Want to try using Ask Learn to clarify or guide you through this topic?