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 from a source object URL using theAzure Storage client library for Python. 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 thePut Blob From URL andPut 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, seeCopy a blob with asynchronous scheduling using Python.
To learn about copying blobs using asynchronous APIs, seeCopy a blob from a source object URL asynchronously.
If you don't have an existing project, this section shows you how to set up a project to work with the Azure Blob Storage client library for Python. For more details, seeGet started with Azure Blob Storage and Python.
To work with the code examples in this article, follow these steps to set up your project.
Install the following packages usingpip install:
pip install azure-storage-blob azure-identityAdd the followingimport statements:
from azure.identity import DefaultAzureCredentialfrom azure.storage.blob import ( BlobServiceClient, BlobClient,)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 roleStorage Blob Data Contributor or higher. To learn more, see the authorization guidance forPut Blob From URL (REST API) orPut Block From URL (REST API).
To connect an app to Blob Storage, create an instance ofBlobServiceClient. The following example shows how to create a client object usingDefaultAzureCredential for authorization:
# TODO: Replace <storage-account-name> with your actual storage account nameaccount_url = "https://<storage-account-name>.blob.core.windows.net"credential = DefaultAzureCredential()# Create the BlobServiceClient objectblob_service_client = BlobServiceClient(account_url, credential=credential)You can also create client objects for specificcontainers orblobs, either directly or from theBlobServiceClient object. To learn more about creating and managing client objects, seeCreate and manage client objects that interact with data resources.
ThePut 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.
ThePut 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 thePut Block From URL API along withPut Block List.
To learn more about thePut Blob From URL operation, including blob size limitations and billing considerations, seePut Blob From URL remarks.
This section gives an overview of methods provided by the Azure Storage client library for Python to perform a copy operation from a source object URL.
The following method wraps thePut Blob From URL REST API operation, and creates a new block blob where the contents of the blob are read from a given URL:
These methods are 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 may choose to work with individual blocks. The following method wraps thePut 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:
If you're copying a blob from a source within Azure, access to the source blob can be authorized via Microsoft Entra ID, a shared access signature (SAS), or an account key.
The following example shows a scenario for copying a source blob within Azure. Theupload_blob_from_url method can optionally accept a Boolean parameter to indicate whether an existing blob should be overwritten, as shown in the example.
def copy_from_source_in_azure(self, source_blob: BlobClient, destination_blob: BlobClient): # Get the source blob URL and create the destination blob # set overwrite param to True if you want to overwrite existing blob data destination_blob.upload_blob_from_url(source_url=source_blob.url, overwrite=False)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.
def copy_from_external_source(self, source_url: str, destination_blob: BlobClient): # Create the destination blob from the source URL # set overwrite param to True if you want to overwrite existing blob data destination_blob.upload_blob_from_url(source_url=source_url, overwrite=False)The Azure Blob Storage client library for Python supports copying a blob from a source URL asynchronously. To learn more about project setup requirements, seeAsynchronous programming.
Follow these steps to copy a blob from a source object URL using asynchronous APIs:
Add the following import statements:
import asynciofrom azure.identity.aio import DefaultAzureCredentialfrom azure.storage.blob.aio import BlobServiceClient, BlobClientAdd code to run the program usingasyncio.run. This function runs the passed coroutine,main() in our example, and manages theasyncio event loop. Coroutines are declared with the async/await syntax. In this example, themain() coroutine first creates the top levelBlobServiceClient usingasync with, then calls the method that copies a blob from a source URL. Note that only the top level client needs to useasync with, as other clients created from it share the same connection pool.
async def main(): sample = BlobCopySamples() # TODO: Replace <storage-account-name> with your actual storage account name account_url = "https://<storage-account-name>.blob.core.windows.net" credential = DefaultAzureCredential() async with BlobServiceClient(account_url, credential=credential) as blob_service_client: # Copy a blob from one container to another in the same storage account source = blob_service_client.get_blob_client(container="source-container", blob="sample-blob.txt") destination = blob_service_client.get_blob_client(container="destination-container", blob="sample-blob.txt") await sample.copy_from_source_in_azure(source_blob=source, destination_blob=destination)if __name__ == '__main__': asyncio.run(main())Add code to copy a blob from a source URL. The following code example is the same as the synchronous example, except that the method is declared with theasync keyword and theawait keyword is used when calling theupload_blob_from_url method.
async def copy_from_source_in_azure(self, source_blob: BlobClient, destination_blob: BlobClient): # Get the source blob URL and create the destination blob # set overwrite param to True if you want to overwrite existing blob data await destination_blob.upload_blob_from_url(source_url=source_blob.url, overwrite=False)With this basic setup in place, you can implement other examples in this article as coroutines using async/await syntax.
To learn more about copying blobs using the Azure Blob Storage client library for Python, see the following resources.
The Azure SDK for Python contains libraries that build on top of the Azure REST API, allowing you to interact with REST API operations through familiar Python 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?