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

Azure Files: Download to a File with Streaming

See more Azure Cloud Storage Examples

Read response headers before writing bytes. Only HTTP 200 is streamed into a staging file, then renamed after completion. The final filename must not already exist; create qa_output first. An interrupted transfer can leave a .part file. This is a fresh download, not a resumable transfer.

Uses direct Chilkat REST calls with HTTPS certificate verification. Shared Key examples load a private qa_data/azure/storage.json file containing string fields account and access_key (the base64 account key). SAS consumers need only account there. Create local input/output directories before running. Examples target Azure public-cloud endpoints; adapt hostnames for other clouds or custom endpoints.

REST examples pin x-ms-version: 2026-04-06, and SAS generation uses the same service version. Complete-URL downloads use the supplied SAS version. This is a fully deployed version documented by Microsoft at review time. Older supported versions are not automatically obsolete. Transport completion and HTTP success are checked separately; writes are not automatically replayed after a lost response. Keep diagnostic dumps, keys and SAS URLs private.

Shared Key remains supported where allowed by account policy. Microsoft recommends Microsoft Entra ID, preferably managed identities where available, and user delegation SAS for delegated Blob access. Account-key SAS and Shared Key requests can be rejected when Shared Key access is disabled; these samples do not bypass that policy. See Azure data authorization.

Microsoft get file reference.

Chilkat PHP Downloads

PHP
<?php

use Chilkat2\AuthAzureStorage;
use Chilkat2\FileAccess;
use Chilkat2\JsonObject;
use Chilkat2\Rest;
use Chilkat2\Socket;
use Chilkat2\Stream;
use Chilkat2\StringBuilder;
use Chilkat2\Xml;

$success = false;

// StringBuilder compares text by value in every generated language.
$textCompare = new StringBuilder();
// Assumes Chilkat is unlocked. See Global Unlock Sample.
$success = false;
$config = new JsonObject();
$config->Strict = true;
try {
    $config->LoadFile('qa_data/azure/storage.json');
} catch (\Chilkat2\Exception $e) {
    echo "Could not load private Azure configuration.\n";
    exit;
}

$textCompare->Clear();
$textCompare->SetString($config->StringOf('account'));
$sameText1 = $textCompare->ContentsEqual('', true);
if (($config->JsonTypeOf('account') != 1) || ($sameText1)) {
    echo "Supply the storage account name.\n";
    exit;
}

// access_key is the base64 account key, not a connection string or SAS.
$textCompare->Clear();
$textCompare->SetString($config->StringOf('access_key'));
$sameText2 = $textCompare->ContentsEqual('', true);
if (($config->JsonTypeOf('access_key') != 1) || ($sameText2)) {
    echo "Supply the base64 account access key.\n";
    exit;
}

$endpoint = new StringBuilder();
try {
    $endpoint->Append($config->StringOf('account'));

    $endpoint->Append('.file.core.windows.net');
} catch (\Chilkat2\Exception $e) {
    echo "Could not prepare the request.\n";
    exit;
}

// resourcePath is already URL-encoded. Encode each path segment once when adapting it.
// Keep slash separators; names and paths are case-sensitive.
$resourcePath = '/examples/files/example.bin';
$rest = new Rest();
$rest->ConnectTimeoutMs = 20000;
$rest->IdleTimeoutMs = 60000;
$azAuth = new AuthAzureStorage();
$azAuth->Account = $config->StringOf('account');
$azAuth->AccessKey = $config->StringOf('access_key');
$azAuth->Service = 'File';
$azAuth->Scheme = 'SharedKey';
$azAuth->XMsVersion = '2026-04-06';
try {
    $rest->SetAuthAzureStorage($azAuth);
} catch (\Chilkat2\Exception $e) {
    echo "Could not configure Shared Key signing.\n";
    exit;
}

$connection = new Socket();
$connection->RequireSslCertVerify = true;
$connection->MaxReadIdleMs = 60000;
$connection->MaxSendIdleMs = 60000;
try {
    $connection->Connect($endpoint->GetAsString(), 443, true, 20000);
} catch (\Chilkat2\Exception $e) {
    echo "TLS connection failed; inspect diagnostics privately.\n";
    exit;
}

// Do not replay a write automatically after losing its response.
try {
    $rest->UseConnection($connection, false);
} catch (\Chilkat2\Exception $e) {
    echo "Could not prepare the request.\n";
    exit;
}

$result = new Xml();
$result->Strict = true;
// Use a new destination and a staging file. A failed transfer is never a completed output.
$files = new FileAccess();
if ($files->FileExists('qa_output/example.bin')) {
    echo "Choose a new output filename.\n";
    exit;
}

$sink = new Stream();
$sink->SinkFile = 'qa_output/example.bin.part';
try {
    $rest->SendReqNoBody('GET', $resourcePath);
} catch (\Chilkat2\Exception $e) {
    echo "Could not send download request.\n";
    exit;
}

$httpStatus = $rest->ReadResponseHeader();
if ($httpStatus < 0) {
    echo "Could not read response headers.\n";
    exit;
}

if ($rest->ResponseStatusCode != 200) {
    echo 'HTTP status: ', $rest->ResponseStatusCode, "\n";
    echo 'Azure error code: ', $rest->ResponseHdrByName('x-ms-error-code'), "\n";
    echo 'Request ID: ', $rest->ResponseHdrByName('x-ms-request-id'), "\n";
    // Inspect any error body privately; do not log credentials, SAS URLs or payloads.
    exit;
}

try {
    $rest->ReadRespBodyStream($sink, false);
} catch (\Chilkat2\Exception $e) {
    echo "Download incomplete; staging file is not a valid final output.\n";
    exit;
}

try {
    $files->FileRename('qa_output/example.bin.part', 'qa_output/example.bin');
} catch (\Chilkat2\Exception $e) {
    echo "Could not publish the downloaded file.\n";
    exit;
}

echo "Download completed to qa_output/example.bin.\n";