Sample code for 30+ languages & platforms
Zig

Azure Storage: List Blobs

See more Azure Cloud Storage Examples

Azure Storage Blob Service REST API: Sample code to fetch the list of blobs in the specified container.

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: List the blobs in a specified container.
    // See also: https://msdn.microsoft.com/en-us/library/azure/dd135734.aspx

    // 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;
    };

    // Provide Azure Cloud credentials for the REST call.
    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("Blob");
    // 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.

    // The expected success response is a 200 response status code with an XML response body.
    // In this example, we are listing the blobs in the container named "mycontainer".
    const response_str = rest.fullRequestNoBody(alloc, "GET", "/mycontainer?restype=container&comp=list") catch {
        std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
        return;
    };

    // When successful, the Azure Storage service will respond with a 200 response status code,
    // with an XML response body.

    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)});
        std.debug.print("response body (if any): {s}\n", .{response_str});
        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;
    }

    // Load the XML response for parsing.
    // An example of the response XML is shown below.
    const xml = try chilkat.Xml.init();
    defer xml.deinit();
    try xml.loadXml(response_str);

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

    // Let's iterate over the blobs...

    const last_mod = try chilkat.DateTime.init();
    defer last_mod.deinit();

    const num_blobs = xml.numChildrenAt("Blobs");
    var i: i32 = 0;
    const dt = try chilkat.DtObj.init();
    defer dt.deinit();
    while (i < num_blobs) {
        std.debug.print("---- Blob {d} ----\n", .{i});
        xml.setI(i);
        std.debug.print("Name: {s}\n", .{try xml.getChildContent(alloc, "Blobs|Blob[i]|Name")});
        std.debug.print("Content-Length: {s}\n", .{try xml.getChildContent(alloc, "Blobs|Blob[i]|Properties|Content-Length")});
        std.debug.print("Content-Type: {s}\n", .{try xml.getChildContent(alloc, "Blobs|Blob[i]|Properties|Content-Type")});
        std.debug.print("Content-MD5: {s}\n", .{try xml.getChildContent(alloc, "Blobs|Blob[i]|Properties|Content-MD5")});
        std.debug.print("BlobType: {s}\n", .{try xml.getChildContent(alloc, "Blobs|Blob[i]|Properties|BlobType")});
        std.debug.print("LeaseStatus: {s}\n", .{try xml.getChildContent(alloc, "Blobs|Blob[i]|Properties|LeaseStatus")});
        last_mod.setFromRfc822(try xml.getChildContent(alloc, "Blobs|Blob[i]|Properties|Last-Modified")) catch {};
        last_mod.toDtObj(true, dt);

        std.debug.print("Last-Modified YMD: {d},{d},{d}\n", .{ dt.getYear(), dt.getMonth(), dt.getDay() });
        i = i + 1;
    }

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

    // Use the ChilkatPath method to get information for a specific blob:

    // For example, let's say we want the Last-Modified date/time for
    // the blob named "helloWorld.txt"
    const last_mod_str = try xml.chilkatPath(alloc, "/C/Name,helloWorld.txt|..|Properties|Last-Modified|*");
    std.debug.print("helloWorld.txt Last-Modified: {s}\n", .{last_mod_str});

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

    // <?xml version="1.0" encoding="utf-8" ?>
    // <EnumerationResults ServiceEndpoint="https://chilkat.blob.core.windows.net/" ContainerName="test">
    //     <Blobs>
    //         <Blob>
    //             <Name>hamletC.xml</Name>
    //             <Properties>
    //                 <Last-Modified>Mon, 02 May 2016 23:50:30 GMT</Last-Modified>
    //                 <Etag>0x8D372E48B6D323A</Etag>
    //                 <Content-Length>279658</Content-Length>
    //                 <Content-Type>application/octet-stream</Content-Type>
    //                 <Content-Encoding />
    //                 <Content-Language />
    //                 <Content-MD5>4E6prI9haw81xadSLeEj3Q==</Content-MD5>
    //                 <Cache-Control />
    //                 <Content-Disposition>attachment; filename="hamletC.xml"</Content-Disposition>
    //                 <BlobType>BlockBlob</BlobType>
    //                 <LeaseStatus>unlocked</LeaseStatus>
    //                 <LeaseState>available</LeaseState>
    //             </Properties>
    //         </Blob>
    //         <Blob>
    //             <Name>helloWorld.txt</Name>
    //             <Properties>
    //                 <Last-Modified>Mon, 02 May 2016 15:21:55 GMT</Last-Modified>
    //                 <Etag>0x8D3729D7EE38299</Etag>
    //                 <Content-Length>12</Content-Length>
    //                 <Content-Type>application/octet-stream</Content-Type>
    //                 <Content-Encoding />
    //                 <Content-Language />
    //                 <Content-MD5>7Qdih1MuhjZehB6Sv8UNjA==</Content-MD5>
    //                 <Cache-Control />
    //                 <Content-Disposition>attachment; filename="helloWorld.txt"</Content-Disposition>
    //                 <BlobType>BlockBlob</BlobType>
    //                 <LeaseStatus>unlocked</LeaseStatus>
    //                 <LeaseState>available</LeaseState>
    //             </Properties>
    //         </Blob>
    //         <Blob>
    //             <Name>thisIsATest.txt</Name>
    //             <Properties>
    //                 <Last-Modified>Tue, 03 May 2016 00:22:13 GMT</Last-Modified>
    //                 <Etag>0x8D372E8F9B2A62E</Etag>
    //                 <Content-Length>1600</Content-Length>
    //                 <Content-Type>application/octet-stream</Content-Type>
    //                 <Content-Encoding />
    //                 <Content-Language />
    //                 <Content-MD5>FhgUXYfePg2ItsraPmz0Xg==</Content-MD5>
    //                 <Cache-Control />
    //                 <Content-Disposition>attachment; filename="thisIsATest.txt"</Content-Disposition>
    //                 <BlobType>BlockBlob</BlobType>
    //                 <LeaseStatus>unlocked</LeaseStatus>
    //                 <LeaseState>available</LeaseState>
    //             </Properties>
    //         </Blob>
    //     </Blobs>
    //     <NextMarker />
    // </EnumerationResults>
    //
}