Sample code for 30+ languages & platforms
Zig Requires Chilkat v11.0.0+

Upload a Blob using a Container’s Shared Access Signature

See more Azure Cloud Storage Examples

Shows how to upload an Azure blob using a URL with a shared access signature.

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

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

    // To upload a file we're simply sending a PUT with the content of the data.  For example:

    // PUT https://myaccount.blob.core.windows.net/pictures/photo.jpg?sv=2015-02-21&st=2015-07-01T08%3a49Z&se=2015-07-02T08%3a49Z&  sr=c&sp=w&si=YWJjZGVmZw%3d%3d&sig=Rcp6gQRfV7WDlURdVTqCa%2bqEArnfJxDgE%2bKH3TCChIs%3d HTTP/1.1
    // Host: myaccount.blob.core.windows.net
    //
    // Content-Length: 12
    //
    // Hello World.

    // Upload a string to a helloWorld.txt file in the "mycontainer" container, in the "chilkat" account.
    const url = "https://chilkat.blob.core.windows.net/mycontainer/helloWorld.txt?sv=2021-06-08&ss=bfqt&srt=sco&sp=rwdlacupiyx&se=2023-02-12T21:30:53Z&st=2023-02-12T13:30:53Z&spr=https&sig=HB8CoZiD7EJD1rQNIVnLl%2Bq7kyLcOCnSXR14TadBv6s%3D";

    const http = try chilkat.Http.init();
    defer http.deinit();

    http.setRequestHeader("x-ms-blob-type", "BlockBlob");

    const resp = try chilkat.HttpResponse.init();
    defer resp.deinit();
    http.httpStr("PUT", url, "Hello World.", "utf-8", "text/plain", resp) catch {
        std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
        return;
    };

    var status_code: i32 = resp.getStatusCode();
    std.debug.print("response status code: {d}\n", .{status_code});
    std.debug.print("response status text: {s}\n", .{try resp.getStatusText(alloc)});

    if (status_code == 201) {
        std.debug.print("Success.\n", .{});
    } else {
        std.debug.print("Failed.\n", .{});
    }

    // -------------------------------------------------------------------------------------------------
    // Upload from a file...

    // Upload a binar file to a penguins.jpg file in the "mycontainer" container, in the "chilkat" account.
    const url2 = "https://chilkat.blob.core.windows.net/mycontainer/penguins.jpg?sv=2021-06-08&ss=bfqt&srt=sco&sp=rwdlacupiyx&se=2023-02-12T21:30:53Z&st=2023-02-12T13:30:53Z&spr=https&sig=HB8CoZiD7EJD1rQNIVnLl%2Bq7kyLcOCnSXR14TadBv6s%3D";

    http.clearHeaders();
    http.setRequestHeader("x-ms-blob-type", "BlockBlob");

    // Upload a JPG file
    http.httpFile("PUT", url2, "c:/qa_data/jpg/penguins.jpg", "image/jpeg", resp) catch {
        std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
        return;
    };

    status_code = resp.getStatusCode();
    std.debug.print("response status code: {d}\n", .{status_code});
    std.debug.print("response status text: {s}\n", .{try resp.getStatusText(alloc)});

    if (status_code == 201) {
        std.debug.print("Success.\n", .{});
    } else {
        std.debug.print("Failed.\n", .{});
    }
}