Επεξεργασία

Κοινή χρήση μέσω


Copy a blob from a source object URL with Java

This article shows how to copy a blob from a source object URL using the Azure Storage client library for Java. 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 Java.

Prerequisites

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 Java. For more information, see Get started with Azure Blob Storage and Java.

To work with the code examples in this article, follow these steps to set up your project.

Note

This article uses the Maven build tool to build and run the example code. Other build tools, such as Gradle, also work with the Azure SDK for Java.

Install packages

Open the pom.xml file in your text editor. Install the packages by including the BOM file, or including a direct dependency.

Add import statements

Add the following import statements:

import com.azure.storage.blob.*;
import com.azure.storage.blob.specialized.*;

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 uses BlobServiceClientBuilder to build a BlobServiceClient object using DefaultAzureCredential, and shows how to create container and blob clients, if needed:

// Azure SDK client builders accept the credential as a parameter
// TODO: Replace <storage-account-name> with your actual storage account name
BlobServiceClient blobServiceClient = new BlobServiceClientBuilder()
        .endpoint("https://<storage-account-name>.blob.core.windows.net/")
        .credential(new DefaultAzureCredentialBuilder().build())
        .buildClient();

// If needed, you can create a BlobContainerClient object from the BlobServiceClient
BlobContainerClient containerClient = blobServiceClient
        .getBlobContainerClient("<container-name>");

// If needed, you can create a BlobClient object from the BlobContainerClient
BlobClient blobClient = containerClient
        .getBlobClient("<blob-name>");

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 source object is a block blob, all committed blob content is copied. However, the block list isn't preserved, and uncommitted blocks aren't copied. The content of the destination blob is identical to that of the source, but the committed block list isn't preserved.

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 Java to perform a copy operation from a source object URL.

The following methods wrap the Put Blob From URL REST API operation, and create 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 can 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 from a source blob within Azure. The uploadFromUrl method can optionally accept a Boolean parameter to indicate whether an existing blob should be overwritten, as shown in the example.

public void copyFromSourceInAzure(BlobClient sourceBlob, BlockBlobClient destinationBlob) {
    // Get the source blob URL and create the destination blob
    // set overwrite param to true if you want to overwrite an existing blob
    destinationBlob.uploadFromUrl(sourceBlob.getBlobUrl(), false);
}

The uploadFromUrlWithResponse method can also accept a BlobUploadFromUrlOptions parameter to specify further options for the operation.

Copy a blob from an external source

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.

public void copyFromExternalSource(String sourceURL, BlockBlobClient destinationBlob) {
    // Create the destination blob from the source URL
    // set overwrite param to true if you want to overwrite an existing blob
    destinationBlob.uploadFromUrl(sourceURL, false);
}

Resources

To learn more about copying blobs using the Azure Blob Storage client library for Java, see the following resources.

Code samples

REST API operations

The Azure SDK for Java contains libraries that build on top of the Azure REST API, allowing you to interact with REST API operations through familiar Java paradigms. The client library methods covered in this article use the following REST API operations:

Client library resources

  • This article is part of the Blob Storage developer guide for Java. To learn more, see the full list of developer guide articles at Build your Java app.