Zig
Zig
Azure Blob Service - List all Containers in a Storage Account
See more Azure Cloud Storage Examples
Azure Storage Blob Service REST API: Lists all the containers in a storage account.Chilkat Zig Downloads
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 all of the containers in an account.
// 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);
// URI parameters, such as "maxresults", are added by calling AddQueryParam
try rest.addQueryParam("maxresults", "1000");
// Note: The application does not need to explicitly set the following
// headers: x-ms-date, Authorization. These headers
// are automatically set by Chilkat.
const response_str = rest.fullRequestNoBody(alloc, "GET", "/?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 code,
// with an XML 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.
// A sample XML response containing 2 containers 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)});
// Iterate over the containers, listing some information about each.
try xml.findChild2("Containers");
const num_containers = xml.getNumChildren();
var i: i32 = 0;
while (i < num_containers) {
try xml.getChild2(i);
std.debug.print("Container Name: {s}\n", .{try xml.getChildContent(alloc, "Name")});
const last_mod_date_str = try xml.chilkatPath(alloc, "Properties|Last-Modified|*");
std.debug.print("Last Modified: {s}\n", .{last_mod_date_str});
// The date/time string can be loaded into a CkDateTime object for conversion to other date formats..
const date_time = try chilkat.DateTime.init();
defer date_time.deinit();
try date_time.setFromRfc822(last_mod_date_str);
const lease_state = try xml.chilkatPath(alloc, "Properties|LeaseState|*");
std.debug.print("Lease State: {s}\n", .{lease_state});
try xml.getParent2();
i = i + 1;
}
try xml.getParent2();
// Sample XML response:
// <?xml version="1.0" encoding="utf-8" ?>
// <EnumerationResults ServiceEndpoint="https://chilkat.blob.core.windows.net/">
// <MaxResults>1000</MaxResults>
// <Containers>
// <Container>
// <Name>test</Name>
// <Properties>
// <Last-Modified>Wed, 20 Apr 2016 01:02:25 GMT</Last-Modified>
// <Etag>"0x8D368B77024C583"</Etag>
// <LeaseStatus>unlocked</LeaseStatus>
// <LeaseState>available</LeaseState>
// </Properties>
// </Container>
// <Container>
// <Name>test2</Name>
// <Properties>
// <Last-Modified>Tue, 03 May 2016 17:13:01 GMT</Last-Modified>
// <Etag>"0x8D373762EB6AC85"</Etag>
// <LeaseStatus>unlocked</LeaseStatus>
// <LeaseState>available</LeaseState>
// </Properties>
// </Container>
// </Containers>
// <NextMarker />
// </EnumerationResults>
}