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

Azure Storage: Create a Short-Lived Account SAS for Blob Writes

See more Azure Cloud Storage Examples

Create an HTTPS-only account SAS for Blob object writes (ss=b, srt=o, sp=w). It does not grant listing or deletion, but it still covers matching objects across the account; it is not constrained to one container or blob. Prefer a narrowly scoped service/user delegation SAS when that is the intended scope. Account SAS cannot refer to a stored access policy.

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 account sas reference.

Chilkat Android™ Downloads

Android™
// Important: Don't forget to include the call to System.loadLibrary
// as shown at the bottom of this code sample.
package com.test;

import android.app.Activity;
import com.chilkatsoft.*;

import android.widget.TextView;
import android.os.Bundle;

public class SimpleActivity extends Activity {

  private static final String TAG = "Chilkat";

  // Called when the activity is first created.
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    boolean success = false;

    //  StringBuilder compares text by value in every generated language.
    CkStringBuilder textCompare = new CkStringBuilder();
    //  Assumes Chilkat is unlocked. See Global Unlock Sample.
    success = false;
    CkJsonObject config = new CkJsonObject();
    config.put_Strict(true);
    success = config.LoadFile("qa_data/azure/storage.json");
    if (success != true) {
        Log.i(TAG, "Could not load private Azure configuration.");
        return;
        }

    textCompare.Clear();
    textCompare.SetString(config.stringOf("account"));
    boolean sameText1 = textCompare.ContentsEqual("",true);
    if ((config.JsonTypeOf("account") != 1) or (sameText1 == true)) {
        Log.i(TAG, "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) or (sameText2 == true)) {
        Log.i(TAG, "Supply the base64 account access key.");
        return;
        }

    CkAuthAzureSAS sas = new CkAuthAzureSAS();
    sas.put_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.put_StringToSign("account,sp,ss,srt,st,se,sip,spr,sv,ses,");
    success = sas.SetNonTokenParam("account",config.stringOf("account"));
    if (success != true) {
        Log.i(TAG, "Could not prepare the request.");
        return;
        }

    success = sas.SetTokenParam("ss","ss","b");
    if (success != true) {
        Log.i(TAG, "Could not prepare the request.");
        return;
        }

    success = sas.SetTokenParam("srt","srt","o");
    if (success != true) {
        Log.i(TAG, "Could not prepare the request.");
        return;
        }

    success = sas.SetTokenParam("sp","sp","w");
    if (success != true) {
        Log.i(TAG, "Could not prepare the request.");
        return;
        }

    success = sas.SetTokenParam("spr","spr","https");
    if (success != true) {
        Log.i(TAG, "Could not prepare the request.");
        return;
        }

    success = sas.SetTokenParam("sv","sv","2026-04-06");
    if (success != true) {
        Log.i(TAG, "Could not prepare the request.");
        return;
        }

    CkDateTime clock = new CkDateTime();
    success = clock.SetFromCurrentSystemTime();
    if (success != true) {
        Log.i(TAG, "Could not prepare the request.");
        return;
        }

    success = clock.AddSeconds(-300);
    if (success != true) {
        Log.i(TAG, "Could not prepare the request.");
        return;
        }

    success = sas.SetTokenParam("st","st",clock.getAsTimestamp(false));
    if (success != true) {
        Log.i(TAG, "Could not prepare the request.");
        return;
        }

    success = clock.AddSeconds(1200);
    if (success != true) {
        Log.i(TAG, "Could not prepare the request.");
        return;
        }

    success = sas.SetTokenParam("se","se",clock.getAsTimestamp(false));
    if (success != true) {
        Log.i(TAG, "Could not prepare the request.");
        return;
        }

    //  Start is five minutes ago; expiry is fifteen minutes from now.
    //  Respect any stricter account SAS-expiration policy.
    String token = sas.generateToken();
    if (sas.get_LastMethodSuccess() != true) {
        Log.i(TAG, "Could not generate SAS.");
        return;
        }

    CkFileAccess files = new CkFileAccess();
    success = files.WriteEntireTextFile("qa_data/azure/account-sas.txt",token,"utf-8",false);
    if (success != true) {
        Log.i(TAG, "Could not save token privately.");
        return;
        }

    Log.i(TAG, "SAS saved privately; do not print the token.");

  }

  static {
      System.loadLibrary("chilkat");

      // Note: If the incorrect library name is passed to System.loadLibrary,
      // then you will see the following error message at application startup:
      //"The application <your-application-name> has stopped unexpectedly. Please try again."
  }
}