Sample code for 30+ languages & platforms
Zig

Bunny Sign URL and then Download using Signed URL

See more Bunny CDN Examples

Shows how to sign a URL for BunnyCDN Token Authentication and then use it to download.

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

    var success: bool = false;

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

    const my_security_key = "e7ea8d73-8fa0-44ef-a2cc-91526f7df5ed";

    const url = "https://test.b-cdn.net/sample-pdf-with-images.pdf";

    // Extract the URL components.
    const url_obj = try chilkat.Url.init();
    defer url_obj.deinit();
    url_obj.parseUrl(url) catch {};

    var url_scheme: [:0]const u8 = "https";
    if (!url_obj.getSsl()) {
        url_scheme = "http";
    }

    const url_host = try url_obj.getHost(alloc);
    const url_path = try url_obj.getPath(alloc);

    // Calculate an expiration time 1 hour from the current date/time.
    const exp_time = try chilkat.DateTime.init();
    defer exp_time.deinit();
    exp_time.setFromCurrentSystemTime() catch {};
    exp_time.addSeconds(3600) catch {};
    const expires = try exp_time.getAsUnixTimeStr(alloc, false);

    std.debug.print("Expires = {s}\n", .{expires});

    // Create the string to hash
    const sb_to_hash = try chilkat.StringBuilder.init();
    defer sb_to_hash.deinit();
    sb_to_hash.append(my_security_key) catch {};
    sb_to_hash.append(url_path) catch {};
    sb_to_hash.append(expires) catch {};

    // Base64Url encoding is the same as base64, except "-" is used instead of "+",
    // "_" is used instead of "/", and no "=" padding is added.
    const token = try sb_to_hash.getHash(alloc, "sha256", "base64Url", "utf-8");

    const sb_signed_url = try chilkat.StringBuilder.init();
    defer sb_signed_url.deinit();
    sb_signed_url.append(url_scheme) catch {};
    sb_signed_url.append("://") catch {};
    sb_signed_url.append(url_host) catch {};
    sb_signed_url.append(url_path) catch {};
    sb_signed_url.append("?token=") catch {};
    sb_signed_url.append(token) catch {};
    sb_signed_url.append("&expires=") catch {};
    sb_signed_url.append(expires) catch {};

    const signed_url = try sb_signed_url.getAsString(alloc);
    std.debug.print("Signed URL: {s}\n", .{signed_url});

    // Use the signed URL to download the file.
    const http = try chilkat.Http.init();
    defer http.deinit();
    success = if (http.download(signed_url, "c:/aaworkarea/sample.pdf")) true else |_| false;
    if (!success) {
        std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
    } else {
        std.debug.print("Success.\n", .{});
    }
}