Sample code for 30+ languages & platforms
Zig

Download a Binary File into Memory

See more Azure Cloud Storage Examples

Downloads a binary file into memory.

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 File Service Example: Download a Binary File to Memory
    // See:  https://docs.microsoft.com/en-us/rest/api/storageservices/get-file

    // 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.file.core.windows.net", port, b_tls, b_auto_reconnect) catch {
        std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
        return;
    };

    // Provide Azure Cloud credentials for the REST calls.
    const az_auth = try chilkat.AuthAzureStorage.init();
    defer az_auth.deinit();
    az_auth.setAccessKey("AZURE_ACCESS_KEY");
    // The account name used here should match the 1st part of the domain passed in the call to Connect (above).
    az_auth.setAccount("chilkat");
    az_auth.setScheme("SharedKey");
    az_auth.setService("File");
    // This causes the "x-ms-version: 2021-08-06" header to be automatically added.
    az_auth.setXMsVersion("2021-08-06");
    try rest.setAuthAzureStorage(az_auth);

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

    // Send a GET request to download the file "penguins.jpg" to a BinData object.
    // (The share name is "pip")
    const bd_file_contents = try chilkat.BinData.init();
    defer bd_file_contents.deinit();
    rest.fullRequestNoBodyBd("GET", "/pip/wildlife/antarctic/penguins.jpg", bd_file_contents) catch {
        std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
        return;
    };

    // When successful, the response status will be 200.
    // If a non-success status code is returned, then the bdFileContents actually contains the XML error message.
    if (rest.getResponseStatusCode() != 200) {
        // 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)});
        const sb_xml_err = try chilkat.StringBuilder.init();
        defer sb_xml_err.deinit();
        sb_xml_err.appendBd(bd_file_contents, "utf-8", 0, 0) catch {};
        std.debug.print("response body (if any): {s}\n", .{try sb_xml_err.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)});
        return;
    }

    std.debug.print("Size of the downloaded file = {d}\n", .{bd_file_contents.getNumBytes()});

    // See the online reference documentation for the BinData class for ways
    // to access the bytes.

    bd_file_contents.writeFile("qa_output/penguins.jpg") catch {};
}