Azure Storage: Create a Short-Lived Service SAS for One Blob
See more Azure Cloud Storage Examples
Create an HTTPS-only service SAS for writing exactly /examplecontainer/example.bin. The canonical resource is URL-decoded and includes /blob/account/container/blob. This is a Blob service SAS, not a universal layout for Files, Tables or Queues. It uses the modern signature layout including the encryption-scope field, even when empty. Prefer user delegation SAS when Entra-based delegation is available.
The token starts five minutes in the past for clock skew and expires fifteen minutes from now; account policy may require a shorter interval. Save it to a private token file and never log it. Generating a token locally does not validate the key, permissions, resource existence or account policy. Protect the signing account key and prefer short-lived delegation.
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 create service sas reference.
Chilkat PHP ActiveX Downloads
<?php
$success = 0;
// StringBuilder compares text by value in every generated language.
$textCompare = new COM("Chilkat.StringBuilder");
// Assumes Chilkat is unlocked. See Global Unlock Sample.
$success = 0;
$config = new COM("Chilkat.JsonObject");
$config->Strict = 1;
$success = $config->LoadFile('qa_data/azure/storage.json');
if ($success != 1) {
print 'Could not load private Azure configuration.' . "\n";
exit;
}
$textCompare->Clear();
$textCompare->SetString($config->stringOf('account'));
$sameText1 = $textCompare->ContentsEqual('',1);
if (($config->JsonTypeOf('account') != 1) or ($sameText1 == 1)) {
print '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('',1);
if (($config->JsonTypeOf('access_key') != 1) or ($sameText2 == 1)) {
print 'Supply the base64 account access key.' . "\n";
exit;
}
$sas = new COM("Chilkat.AuthAzureSAS");
$sas->AccessKey = $config->stringOf('access_key');
// Commas represent LF separators; omitted optional fields still occupy their positions.
// This layout includes signedEncryptionScope (ses), required since version 2020-12-06.
$sas->StringToSign = 'sp,st,se,resource,si,sip,spr,sv,sr,snapshot,ses,rscc,rscd,rsce,rscl,rsct';
$canonical = new COM("Chilkat.StringBuilder");
$success = $canonical->Append('/blob/');
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
exit;
}
$success = $canonical->Append($config->stringOf('account'));
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
exit;
}
$success = $canonical->Append('/examplecontainer/example.bin');
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
exit;
}
$success = $sas->SetNonTokenParam('resource',$canonical->getAsString());
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
exit;
}
$success = $sas->SetTokenParam('sr','sr','b');
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
exit;
}
$success = $sas->SetTokenParam('sp','sp','w');
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
exit;
}
$success = $sas->SetTokenParam('spr','spr','https');
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
exit;
}
$success = $sas->SetTokenParam('sv','sv','2026-04-06');
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
exit;
}
$clock = new COM("Chilkat.CkDateTime");
$success = $clock->SetFromCurrentSystemTime();
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
exit;
}
$success = $clock->AddSeconds(-300);
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
exit;
}
$success = $sas->SetTokenParam('st','st',$clock->getAsTimestamp(0));
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
exit;
}
$success = $clock->AddSeconds(1200);
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
exit;
}
$success = $sas->SetTokenParam('se','se',$clock->getAsTimestamp(0));
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
exit;
}
// Start is five minutes ago; expiry is fifteen minutes from now.
// Respect any stricter account SAS-expiration policy.
$token = $sas->generateToken();
if ($sas->LastMethodSuccess != 1) {
print 'Could not generate SAS.' . "\n";
exit;
}
$files = new COM("Chilkat.FileAccess");
$success = $files->WriteEntireTextFile('qa_data/azure/blob-sas.txt',$token,'utf-8',0);
if ($success != 1) {
print 'Could not save token privately.' . "\n";
exit;
}
print 'SAS saved privately; do not print the token.' . "\n";
?>