Sample code for 30+ languages & platforms
C# Requires Chilkat v11.6.0+

S3: List Objects Under a Prefix

See more S3 - HTTP Examples

Uses Chilkat.Http with Amazon S3 general purpose buckets and Signature Version 4. These examples do not cover directory buckets/S3 Express, access-point ARNs, Outposts, or other specialized endpoints. They target the commercial AWS partition; other partitions need their documented endpoint suffix.

Load private qa_data/aws/s3.json containing nonempty string fields access_key and secret_key, plus session_token for temporary credentials. Prefer short-lived role credentials. These samples do not discover AWS profiles or refresh credentials. Set the bucket's actual region and replace sample bucket names and object keys. TLS certificate verification is explicitly enabled. Keep detailed HTTP diagnostics private because they can contain credentials or presigned URLs.

Uses S3_ListBucketObjects with list-type=2 to request ListObjectsV2. Follows opaque NextContinuationToken values while IsTruncated is true, including empty pages; detects missing/repeated tokens and stops at 10,000 pages with incomplete-results status. Requires s3:ListBucket.

Requests encoding-type=url so keys containing XML-incompatible characters can be represented. Decode returned keys exactly once before reusing them. Query values are encoded separately. Sizes remain decimal strings to preserve large values; LastModified is an ISO 8601 UTC timestamp, not RFC 822. Owner.DisplayName is not used. Listing pages are not a single snapshot if objects change during enumeration.

A prefix is a key-name filter, not a filesystem folder. The trailing slash matters: images/ excludes keys such as images-old/file.jpg. Without a delimiter, all matching descendant keys are included.

AWS reference

Chilkat C# Downloads

C#
bool success = false;

Chilkat.StringBuilder stringCompare = new Chilkat.StringBuilder();
//  This example requires the Chilkat API to have been previously unlocked.
//  See Global Unlock Sample for sample code.
//  Load private credentials; never put access keys or session tokens in source.
Chilkat.JsonObject credentials = new Chilkat.JsonObject();
credentials.Strict = true;
success = credentials.LoadFile("qa_data/aws/s3.json");
if (success != true) {
    Debug.WriteLine("Could not load private S3 credentials.");
    return;
}

stringCompare.SetString(credentials.StringOf("access_key"));
bool stringMatch1 = stringCompare.ContentsEqual("",true);
if ((credentials.JsonTypeOf("access_key") != 1) || (stringMatch1 == true)) {
    Debug.WriteLine("Missing or invalid access_key.");
    return;
}

stringCompare.SetString(credentials.StringOf("secret_key"));
bool stringMatch2 = stringCompare.ContentsEqual("",true);
if ((credentials.JsonTypeOf("secret_key") != 1) || (stringMatch2 == true)) {
    Debug.WriteLine("Missing or invalid secret_key.");
    return;
}

Chilkat.Http http = new Chilkat.Http();
http.AwsAccessKey = credentials.StringOf("access_key");
http.AwsSecretKey = credentials.StringOf("secret_key");
if (credentials.HasMember("session_token") == true) {
    stringCompare.SetString(credentials.StringOf("session_token"));
    bool stringMatch3 = stringCompare.ContentsEqual("",true);
    if ((credentials.JsonTypeOf("session_token") != 1) || (stringMatch3 == true)) {
        Debug.WriteLine("session_token must be a nonempty string when supplied.");
        return;
    }

    http.AwsSessionToken = credentials.StringOf("session_token");
}

//  Current Chilkat uses SigV4 by default. Do not select deprecated SigV2.
//  Set the actual bucket region, not the caller's location.
string region = "us-west-2";
http.AwsRegion = region;
Chilkat.StringBuilder endpoint = new Chilkat.StringBuilder();
endpoint.Append("s3.");
endpoint.Append(region);
endpoint.Append(".amazonaws.com");
http.AwsEndpoint = endpoint.GetAsString();
http.RequireSslCertVerify = true;
http.FollowRedirects = false;
//  A region redirect is not success. Correct the endpoint and signing region.
string bucketName = "your-bucket-name";
Chilkat.StringBuilder bucketPath = new Chilkat.StringBuilder();
Chilkat.StringBuilder encoded = new Chilkat.StringBuilder();
Chilkat.Xml xml = new Chilkat.Xml();
Chilkat.Hashtable seenTokens = new Chilkat.Hashtable();
string token = "";
bool more = true;
int page = 0;
string prefix = "images/";
while ((more == true)) {
    if (page == 10000) {
        Debug.WriteLine("Page limit reached; listing is incomplete.");
        return;
    }

    page = page + 1;
    bucketPath.SetString(bucketName);
    bucketPath.Append("?list-type=2&max-keys=1000&encoding-type=url");
    encoded.SetString(prefix);
    bucketPath.Append("&prefix=");
    bucketPath.Append(encoded.GetEncoded("url","utf-8"));
    stringCompare.SetString(token);
    bool stringMatch4 = stringCompare.ContentsEqual("",true);
    if (stringMatch4 != true) {
        encoded.SetString(token);
        bucketPath.Append("&continuation-token=");
        bucketPath.Append(encoded.GetEncoded("url","utf-8"));
    }

    string xmlStr = http.S3_ListBucketObjects(bucketPath.GetAsString());
    if (http.LastMethodSuccess != true) {
        Debug.WriteLine("Request failed (transport or S3 error). HTTP status: " + Convert.ToString(http.LastStatus));
        //  Inspect LastErrorText privately. Do not log credentials or signed URLs.
        return;
    }

    if (http.LastStatus != 200) {
        Debug.WriteLine("Unexpected HTTP status: " + Convert.ToString(http.LastStatus));
        return;
    }

    success = xml.LoadXml(xmlStr);
    if (success != true) {
        Debug.WriteLine("Invalid XML response.");
        return;
    }

    stringCompare.SetString(xml.Tag);
    bool stringMatch5 = stringCompare.ContentsEqual("ListBucketResult",true);
    if (stringMatch5 != true) {
        Debug.WriteLine("Unexpected XML response root.");
        return;
    }

    stringCompare.SetString(xml.GetChildContent("EncodingType"));
    bool stringMatch6 = stringCompare.ContentsEqual("url",true);
    if (stringMatch6 != true) {
        Debug.WriteLine("Expected URL-encoded listing keys.");
        return;
    }

    int i = 0;
    int count = xml.NumChildrenHavingTag("Contents");
    while ((i < count)) {
        xml.I = i;
        //  Decode keys exactly once. Sizes remain decimal strings (not int).
        string itemKey = http.UrlDecode(xml.GetChildContent("Contents[i]|Key"));
        stringCompare.SetString(itemKey);
        bool stringMatch7 = stringCompare.ContentsEqual("",true);
        if ((http.LastMethodSuccess != true) || (stringMatch7 == true)) {
            Debug.WriteLine("Invalid object key.");
            return;
        }

        string size = xml.GetChildContent("Contents[i]|Size");
        stringCompare.SetString(size);
        bool stringMatch8 = stringCompare.ContentsEqual("",true);
        if ((xml.LastMethodSuccess != true) || (stringMatch8 == true)) {
            Debug.WriteLine("Incomplete object entry.");
            return;
        }

        string modified = xml.GetChildContent("Contents[i]|LastModified");
        stringCompare.SetString(modified);
        bool stringMatch9 = stringCompare.ContentsEqual("",true);
        if ((xml.LastMethodSuccess != true) || (stringMatch9 == true)) {
            Debug.WriteLine("Incomplete object entry.");
            return;
        }

        Debug.WriteLine("Key: " + itemKey);
        Debug.WriteLine("Bytes: " + size);
        //  S3 returns ISO 8601, not RFC 822. Keep the original UTC timestamp.
        Debug.WriteLine("Last modified (UTC): " + modified);
        i = i + 1;
    }

    string truncated = xml.GetChildContent("IsTruncated");
    stringCompare.SetString(truncated);
    bool stringMatch10 = stringCompare.ContentsEqual("true",true);
    stringCompare.SetString(truncated);
    bool stringMatch11 = stringCompare.ContentsEqual("false",true);
    if ((stringMatch10 != true) && (stringMatch11 != true)) {
        Debug.WriteLine("Missing or invalid IsTruncated; listing is incomplete.");
        return;
    }

    more = false;
    stringCompare.SetString(truncated);
    bool stringMatch12 = stringCompare.ContentsEqual("true",true);
    if (stringMatch12 == true) {
        token = xml.GetChildContent("NextContinuationToken");
        stringCompare.SetString(token);
        bool stringMatch13 = stringCompare.ContentsEqual("",true);
        if ((xml.LastMethodSuccess != true) || (stringMatch13 == true)) {
            Debug.WriteLine("Missing continuation token; listing is incomplete.");
            return;
        }

        if (seenTokens.Contains(token) == true) {
            Debug.WriteLine("Repeated continuation token; listing is incomplete.");
            return;
        }

        success = seenTokens.AddStr(token,"seen");
        if (success != true) {
            Debug.WriteLine("Could not track pagination.");
            return;
        }

        more = true;
    }

}

Debug.WriteLine("Object listing complete.");