Copy a blob from a source object URL with Python
This article shows how to copy a blob from a source object URL using the Azure 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 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 Python.
To learn about copying blobs using asynchronous APIs, see Copy a blob from a source object URL asynchronously.
Prerequisites
- Azure subscription - create one for free
- Azure storage account - create a storage account
- Python 3.8+
Set up your environment
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, see Get 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 packages
Install the following packages using pip install
:
pip install azure-storage-blob azure-identity
Add import statements
Add the following import
statements:
from azure.identity import DefaultAzureCredential
from azure.storage.blob import (
BlobServiceClient,
BlobClient,
)
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 (REST API) or Put Block From URL (REST API).
Create a client object
To connect an app to Blob Storage, create an instance of BlobServiceClient. The following example shows how to create a client object using DefaultAzureCredential
for authorization:
# TODO: Replace <storage-account-name> with your actual storage account name
account_url = "https://<storage-account-name>.blob.core.windows.net"
credential = DefaultAzureCredential()
# Create the BlobServiceClient object
blob_service_client = BlobServiceClient(account_url, credential=credential)
You can also create client objects for specific containers or blobs, either directly or from the BlobServiceClient
object. To learn more about creating and managing client objects, see Create and manage client objects that interact with data resources.
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 Python 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:
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 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, a shared access signature (SAS), or an account key.
The following example shows a scenario for copying a source blob within Azure. The upload_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)
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 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)
Copy a blob from a source object URL asynchronously
The Azure Blob Storage client library for Python supports copying a blob from a source URL asynchronously. To learn more about project setup requirements, see Asynchronous programming.
Follow these steps to copy a blob from a source object URL using asynchronous APIs:
Add the following import statements:
import asyncio from azure.identity.aio import DefaultAzureCredential from azure.storage.blob.aio import BlobServiceClient, BlobClient
Add code to run the program using
asyncio.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 the
async
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.
Resources
To learn more about copying blobs using the Azure Blob Storage client library for Python, see the following resources.
Code samples
- View synchronous or asynchronous code samples from this article (GitHub)
REST API operations
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:
- Put Blob From URL (REST API)
- Put Block From URL (REST API)
Client library resources
Related content
- This article is part of the Blob Storage developer guide for Python. To learn more, see the full list of developer guide articles at Build your Python app.