Sample code for 30+ languages & platforms
Zig

Configure Azure SAS Authentication for REST

See more REST Examples

Demonstrates Rest.SetAuthAzureSas, which associates an AuthAzureSAS provider so requests are authorized with an Azure Shared Access Signature token.

Background. A SAS token grants time-limited, permission-scoped access to Azure Storage resources without exposing the account key on the wire. Use SAS authorization when a query-token approach is preferred over Shared Key headers.

Chilkat Zig Downloads

Zig
const std = @import("std");
const chilkat = @import("chilkat");

pub fn main(init: std.process.Init) !void {
    const alloc = init.arena.allocator();

    const rest = try chilkat.Rest.init();
    defer rest.deinit();
    const b_tls = true;
    const b_auto_reconnect = true;
    rest.connect("example.com", 443, b_tls, b_auto_reconnect) catch {
        std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
        return;
    };

    // Configure Azure Shared Access Signature (SAS) authentication.  Provide the account access key
    // and the SAS token query parameters; Chilkat adds the SAS token to each request.
    const az_sas = try chilkat.AuthAzureSAS.init();
    defer az_sas.deinit();
    // Normally you would not hard-code secrets in source.  You should instead obtain them from an
    // interactive prompt, environment variable, or a secrets vault.
    az_sas.setAccessKey("BASE64_ACCOUNT_ACCESS_KEY");

    // Associate the provider with the REST object.
    rest.setAuthAzureSas(az_sas) catch {
        std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
        return;
    };

    const response_text = rest.fullRequestNoBody(alloc, "GET", "/mycontainer/myblob.txt") catch {
        std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("{s}\n", .{response_text});
}