Sample code for 30+ languages & platforms
Zig

Use an Azure Service SAS

See more Azure Cloud Storage Examples

Shows how to use an Azure Service SAS. (In this case, it is an SAS for a blob.)

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();

    // Azure Blob Service Example: Upload binary bytes to a block blob.
    // This uses a Service SAS for Authorization.

    // This example requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    const rest = try chilkat.Rest.init();
    defer rest.deinit();

    // Connect to the Azure Storage Blob Service
    const b_tls = true;
    const port = 443;
    const b_auto_reconnect = true;
    // In this example, the storage account name is "chilkat".
    rest.connect("chilkat.blob.core.windows.net", port, b_tls, b_auto_reconnect) catch {
        std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
        return;
    };

    // ----------------------------------------------------------------------------------------------
    // The code above this comment could be placed inside a function/subroutine within the application
    // because the connection does not need to be made for every request.  Once the connection is made
    // the app may send many requests..
    // ----------------------------------------------------------------------------------------------

    // Note: The application does not need to explicitly set the following
    // headers: Content-Length, x-ms-date, Authorization.  These headers
    // are automatically set by Chilkat.

    // Let's load a previously computed Azure Storage Account SAS token and use it.
    // See How to Create an Azure Service SAS
    const sb_token = try chilkat.StringBuilder.init();
    defer sb_token.deinit();
    sb_token.loadFile("qa_data/tokens/azureStorageServiceSas.txt", "utf-8") catch {
        std.debug.print("Failed to load SAS token.\n", .{});
        return;
    };

    // Add the Shared Access Signature query params for authorization.
    rest.addQueryParams(try sb_token.getAsString(alloc)) catch {};

    // IMPORTANT: Make sure to set the x-ms-blob-type header:
    try rest.addHeader("x-ms-blob-type", "BlockBlob");

    // IMPORTANT: Make sure to add the x-ms-date header.
    // When the header name is "x-ms-date", Chilkat will recognize the keyword "NOW"
    // and will substitute the current system date/time formatted as required by Microsoft.
    try rest.addHeader("x-ms-date", "NOW");

    // For this example, we'll just load a JPG file into memory..
    const bin_data = try chilkat.BinData.init();
    defer bin_data.deinit();
    bin_data.loadFile("qa_data/jpg/starfish.jpg") catch {
        std.debug.print("Failed to load JPG file.\n", .{});
        return;
    };

    // Note: The maximum size of a block blob created by uploading in a single step is 64MB.
    // For larger files, the upload must be broken up into blocks.  There is another Chilkat example for that..
    const sb_response = try chilkat.StringBuilder.init();
    defer sb_response.deinit();
    rest.fullRequestBd("PUT", "/mycontainer/starfish.jpg", bin_data, sb_response) catch {
        std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
        return;
    };

    // When successful, the Azure Storage service will respond with a 201 response status code,
    // with no response body.
    if (rest.getResponseStatusCode() != 201) {
        // Examine the request/response to see what happened.
        std.debug.print("response status code = {d}\n", .{rest.getResponseStatusCode()});
        std.debug.print("response status text = {s}\n", .{try rest.getResponseStatusText(alloc)});
        std.debug.print("response header: {s}\n", .{try rest.getResponseHeader(alloc)});
        std.debug.print("response body (if any): {s}\n", .{try sb_response.getAsString(alloc)});
        std.debug.print("---\n", .{});
        std.debug.print("LastRequestStartLine: {s}\n", .{try rest.getLastRequestStartLine(alloc)});
        std.debug.print("LastRequestHeader: {s}\n", .{try rest.getLastRequestHeader(alloc)});
        std.debug.print("Failed.\n", .{});
        return;
    }

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