Sample code for 30+ languages & platforms
Zig

Bunny Edge Storage - List Files

See more Bunny CDN Examples

Retrieve a list of files and directories located in the given directory.

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 assumes the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

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

    // Use the "Password" from the "FTP & HTTP API Access" console web page for your Bunny storage zone.
    http.setRequestHeader("AccessKey", "YOUR_FTP_AND_HTTP_PASSWORD");

    // Storage Endpoints:
    // The storage API endpoint depends on the primary storage region of your storage zone. You can also find this in the FTP & HTTP API Information of your storage zone.

    // Falkenstein: storage.bunnycdn.com
    // New York: ny.storage.bunnycdn.com
    // Los Angeles: la.storage.bunnycdn.com
    // Singapore: sg.storage.bunnycdn.com
    // Sydney: syd.storage.bunnycdn.com
    // London: uk.storage.bunnycdn.com
    // ... (possibly others??)

    const sb_response = try chilkat.StringBuilder.init();
    defer sb_response.deinit();

    // Change the "storageZoneName" to your actual storage zone name.
    http.quickGetSb("https://uk.storage.bunnycdn.com/storageZoneName/testDir/", sb_response) catch {
        std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
        return;
    };

    const status_code = http.getLastStatus();
    std.debug.print("Status code = {d}\n", .{status_code});

    if (status_code != 200) {
        std.debug.print("{s}\n", .{try sb_response.getAsString(alloc)});
        std.debug.print("Failed.\n", .{});
        return;
    }

    const jarr = try chilkat.JsonArray.init();
    defer jarr.deinit();
    jarr.loadSb(sb_response) catch {};
    jarr.setEmitCompact(false);

    std.debug.print("{s}\n", .{try jarr.emit(alloc)});

    // Here is a sample response.  Parsing code is shown below..

    // [
    //   {
    //     "Guid": "ebd9ccff-faf7-449d-9c71-8662b9f3cf41",
    //     "StorageZoneName": "chilkat",
    //     "Path": "/chilkat/testDir/",
    //     "ObjectName": "seahorse.jpg",
    //     "Length": 24388,
    //     "LastChanged": "2023-04-21T15:59:08.382",
    //     "ServerId": 586,
    //     "ArrayNumber": 0,
    //     "IsDirectory": false,
    //     "UserId": "6d26ab35-2914-422c-9378-d51f38166e53",
    //     "ContentType": "",
    //     "DateCreated": "2023-04-21T15:59:08.382",
    //     "StorageZoneId": 266805,
    //     "Checksum": "0503558E7B4D141C067BEECFD06F87F6F58B81E37918310F82C7C4077D27BD1D",
    //     "ReplicatedZones": "SE,DE,LA,SG,JH,BR,SYD,NY"
    //   },
    //   {
    //     "Guid": "e74b85f8-4d5d-459e-a2fc-0fcfa8c926aa",
    //     "StorageZoneName": "chilkat",
    //     "Path": "/chilkat/testDir/",
    //     "ObjectName": "seal.jpg",
    //     "Length": 342708,
    //     "LastChanged": "2023-04-21T15:59:08.763",
    //     "ServerId": 586,
    //     "ArrayNumber": 2,
    //     "IsDirectory": false,
    //     "UserId": "6d26ab35-2914-422c-9378-d51f38166e53",
    //     "ContentType": "",
    //     "DateCreated": "2023-04-21T15:59:08.763",
    //     "StorageZoneId": 266805,
    //     "Checksum": "C95F946A0299CE39C20A072B3DC581446B2EE2D94CA93901B58604F4EFB8AAB5",
    //     "ReplicatedZones": "DE,SE,SG,LA,JH,BR,SYD,NY"
    //   },
    //   {
    //     "Guid": "ca80cdbd-c6cf-445d-b367-0b33de7ae65a",
    //     "StorageZoneName": "chilkat",
    //     "Path": "/chilkat/testDir/",
    //     "ObjectName": "university.jpg",
    //     "Length": 277581,
    //     "LastChanged": "2023-04-21T15:59:10.142",
    //     "ServerId": 586,
    //     "ArrayNumber": 0,
    //     "IsDirectory": false,
    //     "UserId": "6d26ab35-2914-422c-9378-d51f38166e53",
    //     "ContentType": "",
    //     "DateCreated": "2023-04-21T15:59:10.142",
    //     "StorageZoneId": 266805,
    //     "Checksum": "96A10EAA1B778C066739A6233206022DB219B67A8775401D7865F40E6DD83571",
    //     "ReplicatedZones": "DE,SE,SG,JH,BR,LA,SYD,NY"
    //   }
    // ]

    // Use this online tool to generate parsing code from sample JSON:
    // Generate Parsing Code from JSON

    var guid: [:0]const u8 = "";
    var storage_zone_name: [:0]const u8 = "";
    var path: [:0]const u8 = "";
    var object_name: [:0]const u8 = "";
    var length: i32 = 0;
    var last_changed: [:0]const u8 = "";
    var server_id: i32 = 0;
    var array_number: i32 = 0;
    var is_directory: bool = false;
    var user_id: [:0]const u8 = "";
    var content_type: [:0]const u8 = "";
    var date_created: [:0]const u8 = "";
    var storage_zone_id: i32 = 0;
    var checksum: [:0]const u8 = "";
    var replicated_zones: [:0]const u8 = "";

    var i: i32 = 0;
    const count_i = jarr.getSize();
    while (i < count_i) {
        const json = try jarr.objectAt(i);
        defer json.deinit();
        guid = try json.stringOf(alloc, "Guid");
        storage_zone_name = try json.stringOf(alloc, "StorageZoneName");
        path = try json.stringOf(alloc, "Path");
        object_name = try json.stringOf(alloc, "ObjectName");
        length = json.intOf("Length");
        last_changed = try json.stringOf(alloc, "LastChanged");
        server_id = json.intOf("ServerId");
        array_number = json.intOf("ArrayNumber");
        is_directory = json.boolOf("IsDirectory");
        user_id = try json.stringOf(alloc, "UserId");
        content_type = try json.stringOf(alloc, "ContentType");
        date_created = try json.stringOf(alloc, "DateCreated");
        storage_zone_id = json.intOf("StorageZoneId");
        checksum = try json.stringOf(alloc, "Checksum");
        replicated_zones = try json.stringOf(alloc, "ReplicatedZones");

        i = i + 1;
    }
}