Sample code for 30+ languages & platforms
Java Requires Chilkat v11.6.0+

Azure Blob: Copy a Blob and Track Completion

See more Azure Cloud Storage Examples

Copy a blob within the same account without downloading it into the application. Shared Key authorizes the same-account source and destination. If-None-Match: * prevents replacing an existing destination. Cross-account sources need separate authorization, often a read SAS; never log that URL.

A 202 means accepted, not completed. Retain the returned copy ID and poll destination properties until success, failure or abort. The example waits for at most 20 polls; pending at that point is not success and does not cancel the copy. On restart continue tracking the original ID, rather than issuing another copy. Account type, archive tier, leases and destination conditions can affect copy eligibility.

Uses direct Chilkat.Rest requests and Azure public-cloud endpoints with verified HTTPS. REST requests select service version 2026-04-06. Create local input/output directories before running. Account-key examples read account and access_key string fields from private qa_data/azure/storage.json; do not embed secrets in distributed applications. Entra examples explain their own configuration. Keep credentials, bearer tokens, SAS URLs and diagnostic dumps private. Shared Key is supported only where allowed by account policy; Entra authentication is preferred where available.

Transport success and HTTP status are checked separately. Writes are not automatically replayed after an uncertain response. IDs, ETags, continuation markers and file sizes are strings; counters and explicitly bounded offsets use int. No webhook or web server is required.

Microsoft reference.

Chilkat Java Downloads

Java
import com.chilkatsoft.chilkat2.AuthAzureStorage;
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.JsonObject;
import com.chilkatsoft.chilkat2.Rest;
import com.chilkatsoft.chilkat2.Socket;
import com.chilkatsoft.chilkat2.StringBuilder;
import com.chilkatsoft.chilkat2.Xml;

public class ChilkatExample {

    public static void main(String[] args) {
        boolean success = false;

        // StringBuilder compares text by value in every generated language.
        StringBuilder textCompare = new StringBuilder();
        // Assumes Chilkat is unlocked. See Global Unlock Sample.
        success = false;
        JsonObject config = new JsonObject();
        config.setStrict(true);
        try {
            config.loadFile("qa_data/azure/storage.json");
        } catch (ChilkatException e) {
            System.out.println("Could not load private Azure configuration.");
            return;
        }

        textCompare.clear();
        textCompare.setString(config.stringOf("account"));
        boolean sameText1 = textCompare.contentsEqual("", true);
        if ((config.jsonTypeOf("account") != 1) || (sameText1)) {
            System.out.println("Supply the storage account name.");
            return;
        }

        // access_key is the base64 account key, not a connection string or SAS.
        textCompare.clear();
        textCompare.setString(config.stringOf("access_key"));
        boolean sameText2 = textCompare.contentsEqual("", true);
        if ((config.jsonTypeOf("access_key") != 1) || (sameText2)) {
            System.out.println("Supply the base64 account access key.");
            return;
        }

        StringBuilder endpoint = new StringBuilder();
        try {
            endpoint.append(config.stringOf("account"));

            endpoint.append(".blob.core.windows.net");
        } catch (ChilkatException e) {
            System.out.println("Could not prepare the request.");
            return;
        }

        // resourcePath is already URL-encoded. Encode each path segment once when adapting it.
        // Keep slash separators; names and paths are case-sensitive.
        String resourcePath = "/examplecontainer/example-copy.bin";
        Rest rest = new Rest();
        rest.setConnectTimeoutMs(20000);
        rest.setIdleTimeoutMs(60000);
        AuthAzureStorage azAuth = new AuthAzureStorage();
        azAuth.setAccount(config.stringOf("account"));
        azAuth.setAccessKey(config.stringOf("access_key"));
        azAuth.setService("Blob");
        azAuth.setScheme("SharedKey");
        azAuth.setXMsVersion("2026-04-06");
        try {
            rest.setAuthAzureStorage(azAuth);
        } catch (ChilkatException e) {
            System.out.println("Could not configure Shared Key signing.");
            return;
        }

        Socket connection = new Socket();
        connection.setRequireSslCertVerify(true);
        connection.setMaxReadIdleMs(60000);
        connection.setMaxSendIdleMs(60000);
        try {
            connection.connect(endpoint.getAsString(), 443, true, 20000);
        } catch (ChilkatException e) {
            System.out.println("TLS connection failed; inspect diagnostics privately.");
            return;
        }

        // Do not replay a write automatically after losing its response.
        try {
            rest.useConnection(connection, false);
        } catch (ChilkatException e) {
            System.out.println("Could not prepare the request.");
            return;
        }

        String responseText = "";
        Xml result = new Xml();
        result.setStrict(true);
        StringBuilder sourceUrl = new StringBuilder();
        try {
            sourceUrl.append("https://");

            sourceUrl.append(endpoint.getAsString());

            sourceUrl.append("/examplecontainer/example.bin");

            rest.addHeader("x-ms-copy-source", sourceUrl.getAsString());

            rest.addHeader("If-None-Match", "*");
        } catch (ChilkatException e) {
            System.out.println("Could not prepare the request.");
            return;
        }

        try {
            responseText = rest.fullRequestNoBody("PUT", resourcePath);
        } catch (ChilkatException e) {
            System.out.println("Request did not complete; outcome may be uncertain.");
            return;
        }

        if (rest.getResponseStatusCode() != 202) {
            System.out.println("HTTP status: " + rest.getResponseStatusCode());
            System.out.println("Azure error code: " + rest.responseHdrByName("x-ms-error-code"));
            System.out.println("Request ID: " + rest.responseHdrByName("x-ms-request-id"));
            // Inspect any error body privately; do not log credentials, SAS URLs or payloads.
            return;
        }

        String copyId = rest.responseHdrByName("x-ms-copy-id");
        textCompare.clear();
        textCompare.setString(copyId);
        boolean sameText3 = textCompare.contentsEqual("", true);
        if (sameText3) {
            System.out.println("Copy accepted without a copy ID; inspect before retrying.");
            return;
        }

        System.out.println("Copy ID: " + copyId);
        // Save copyId and destination in application state if polling must survive restart.
        try {
            rest.clearAllHeaders();

            rest.clearAllQueryParams();
        } catch (ChilkatException e) {
            System.out.println("Could not prepare the request.");
            return;
        }

        int polls = 0;
        boolean completed = false;
        while ((polls < 20) && (!completed)) {
            rest.disconnect(10);
            try {
                connection.connect(endpoint.getAsString(), 443, true, 20000);
            } catch (ChilkatException e) {
                System.out.println("TLS reconnection failed.");
                return;
            }

            try {
                rest.useConnection(connection, false);
            } catch (ChilkatException e) {
                System.out.println("Could not prepare the request.");
                return;
            }

            try {
                responseText = rest.fullRequestNoBody("HEAD", resourcePath);
            } catch (ChilkatException e) {
                System.out.println("Request did not complete; outcome may be uncertain.");
                return;
            }

            if (rest.getResponseStatusCode() != 200) {
                System.out.println("HTTP status: " + rest.getResponseStatusCode());
                System.out.println("Azure error code: " + rest.responseHdrByName("x-ms-error-code"));
                System.out.println("Request ID: " + rest.responseHdrByName("x-ms-request-id"));
                // Inspect any error body privately; do not log credentials, SAS URLs or payloads.
                return;
            }

            textCompare.clear();
            textCompare.setString(rest.responseHdrByName("x-ms-copy-id"));
            boolean sameText4 = textCompare.contentsEqual(copyId, true);
            if (!sameText4) {
                System.out.println("Destination no longer reports this copy ID.");
                return;
            }

            String copyStatus = rest.responseHdrByName("x-ms-copy-status");
            textCompare.clear();
            textCompare.setString(copyStatus);
            boolean sameText5 = textCompare.contentsEqual("success", true);
            if (sameText5) {
                completed = true;
            } else {
                textCompare.clear();
                textCompare.setString(copyStatus);
                boolean sameText6 = textCompare.contentsEqual("pending", true);
                if (!sameText6) {
                    System.out.println("Copy failed, was aborted, or returned an unexpected status; inspect copy diagnostics privately.");
                    return;
                }

                // Pause between polls; the copy can outlast this example's bounded wait.
                connection.sleepMs(1000);
            }

            polls = polls + 1;
        }

        if (!completed) {
            System.out.println("Copy is still pending. Continue polling this ID; do not start another copy.");
            return;
        }

        System.out.println("Copy completed.");
    }
}