Sample code for 30+ languages & platforms
Visual FoxPro Requires Chilkat v11.6.0+

S3: List More Than 1,000 Objects with Pagination

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.

AWS reference

Chilkat Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL loStringCompare
LOCAL loCredentials
LOCAL lnStringMatch1
LOCAL lnStringMatch2
LOCAL loHttp
LOCAL lnStringMatch3
LOCAL lcRegion
LOCAL loEndpoint
LOCAL lcBucketName
LOCAL loBucketPath
LOCAL loEncoded
LOCAL loXml
LOCAL loSeenTokens
LOCAL lcToken
LOCAL lnMore
LOCAL lnPage
LOCAL lnStringMatch4
LOCAL lcXmlStr
LOCAL lnStringMatch5
LOCAL lnStringMatch6
LOCAL i
LOCAL lnCount
LOCAL lcItemKey
LOCAL lnStringMatch7
LOCAL lcSize
LOCAL lnStringMatch8
LOCAL lcModified
LOCAL lnStringMatch9
LOCAL lcTruncated
LOCAL lnStringMatch10
LOCAL lnStringMatch11
LOCAL lnStringMatch12
LOCAL lnStringMatch13

lnSuccess = 0

loStringCompare = CreateObject('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.
loCredentials = CreateObject('Chilkat.JsonObject')
loCredentials.Strict = 1
lnSuccess = loCredentials.LoadFile("qa_data/aws/s3.json")
IF (lnSuccess <> 1) THEN
    ? "Could not load private S3 credentials."
    RELEASE loStringCompare
    RELEASE loCredentials
    CANCEL
ENDIF

loStringCompare.SetString(loCredentials.StringOf("access_key"))
lnStringMatch1 = loStringCompare.ContentsEqual("",1)
IF ((loCredentials.JsonTypeOf("access_key") <> 1) OR (lnStringMatch1 = 1)) THEN
    ? "Missing or invalid access_key."
    RELEASE loStringCompare
    RELEASE loCredentials
    CANCEL
ENDIF

loStringCompare.SetString(loCredentials.StringOf("secret_key"))
lnStringMatch2 = loStringCompare.ContentsEqual("",1)
IF ((loCredentials.JsonTypeOf("secret_key") <> 1) OR (lnStringMatch2 = 1)) THEN
    ? "Missing or invalid secret_key."
    RELEASE loStringCompare
    RELEASE loCredentials
    CANCEL
ENDIF

loHttp = CreateObject('Chilkat.Http')
loHttp.AwsAccessKey = loCredentials.StringOf("access_key")
loHttp.AwsSecretKey = loCredentials.StringOf("secret_key")
IF (loCredentials.HasMember("session_token") = 1) THEN
    loStringCompare.SetString(loCredentials.StringOf("session_token"))
    lnStringMatch3 = loStringCompare.ContentsEqual("",1)
    IF ((loCredentials.JsonTypeOf("session_token") <> 1) OR (lnStringMatch3 = 1)) THEN
        ? "session_token must be a nonempty string when supplied."
        RELEASE loStringCompare
        RELEASE loCredentials
        RELEASE loHttp
        CANCEL
    ENDIF

    loHttp.AwsSessionToken = loCredentials.StringOf("session_token")
ENDIF

*  Current Chilkat uses SigV4 by default. Do not select deprecated SigV2.
*  Set the actual bucket region, not the caller's location.
lcRegion = "us-west-2"
loHttp.AwsRegion = lcRegion
loEndpoint = CreateObject('Chilkat.StringBuilder')
loEndpoint.Append("s3.")
loEndpoint.Append(lcRegion)
loEndpoint.Append(".amazonaws.com")
loHttp.AwsEndpoint = loEndpoint.GetAsString()
loHttp.RequireSslCertVerify = 1
loHttp.FollowRedirects = 0
*  A region redirect is not success. Correct the endpoint and signing region.
lcBucketName = "your-bucket-name"
loBucketPath = CreateObject('Chilkat.StringBuilder')
loEncoded = CreateObject('Chilkat.StringBuilder')
loXml = CreateObject('Chilkat.Xml')
loSeenTokens = CreateObject('Chilkat.Hashtable')
lcToken = ""
lnMore = 1
lnPage = 0
DO WHILE (lnMore = 1)
    IF (lnPage = 10000) THEN
        ? "Page limit reached; listing is incomplete."
        RELEASE loStringCompare
        RELEASE loCredentials
        RELEASE loHttp
        RELEASE loEndpoint
        RELEASE loBucketPath
        RELEASE loEncoded
        RELEASE loXml
        RELEASE loSeenTokens
        CANCEL
    ENDIF

    lnPage = lnPage + 1
    loBucketPath.SetString(lcBucketName)
    loBucketPath.Append("?list-type=2&max-keys=1000&encoding-type=url")
    loStringCompare.SetString(lcToken)
    lnStringMatch4 = loStringCompare.ContentsEqual("",1)
    IF (lnStringMatch4 <> 1) THEN
        loEncoded.SetString(lcToken)
        loBucketPath.Append("&continuation-token=")
        loBucketPath.Append(loEncoded.GetEncoded("url","utf-8"))
    ENDIF

    lcXmlStr = loHttp.S3_ListBucketObjects(loBucketPath.GetAsString())
    IF (loHttp.LastMethodSuccess <> 1) THEN
        ? "Request failed (transport or S3 error). HTTP status: " + STR(loHttp.LastStatus)
        *  Inspect LastErrorText privately. Do not log credentials or signed URLs.
        RELEASE loStringCompare
        RELEASE loCredentials
        RELEASE loHttp
        RELEASE loEndpoint
        RELEASE loBucketPath
        RELEASE loEncoded
        RELEASE loXml
        RELEASE loSeenTokens
        CANCEL
    ENDIF

    IF (loHttp.LastStatus <> 200) THEN
        ? "Unexpected HTTP status: " + STR(loHttp.LastStatus)
        RELEASE loStringCompare
        RELEASE loCredentials
        RELEASE loHttp
        RELEASE loEndpoint
        RELEASE loBucketPath
        RELEASE loEncoded
        RELEASE loXml
        RELEASE loSeenTokens
        CANCEL
    ENDIF

    lnSuccess = loXml.LoadXml(lcXmlStr)
    IF (lnSuccess <> 1) THEN
        ? "Invalid XML response."
        RELEASE loStringCompare
        RELEASE loCredentials
        RELEASE loHttp
        RELEASE loEndpoint
        RELEASE loBucketPath
        RELEASE loEncoded
        RELEASE loXml
        RELEASE loSeenTokens
        CANCEL
    ENDIF

    loStringCompare.SetString(loXml.Tag)
    lnStringMatch5 = loStringCompare.ContentsEqual("ListBucketResult",1)
    IF (lnStringMatch5 <> 1) THEN
        ? "Unexpected XML response root."
        RELEASE loStringCompare
        RELEASE loCredentials
        RELEASE loHttp
        RELEASE loEndpoint
        RELEASE loBucketPath
        RELEASE loEncoded
        RELEASE loXml
        RELEASE loSeenTokens
        CANCEL
    ENDIF

    loStringCompare.SetString(loXml.GetChildContent("EncodingType"))
    lnStringMatch6 = loStringCompare.ContentsEqual("url",1)
    IF (lnStringMatch6 <> 1) THEN
        ? "Expected URL-encoded listing keys."
        RELEASE loStringCompare
        RELEASE loCredentials
        RELEASE loHttp
        RELEASE loEndpoint
        RELEASE loBucketPath
        RELEASE loEncoded
        RELEASE loXml
        RELEASE loSeenTokens
        CANCEL
    ENDIF

    i = 0
    lnCount = loXml.NumChildrenHavingTag("Contents")
    DO WHILE (i < lnCount)
        loXml.I = i
        *  Decode keys exactly once. Sizes remain decimal strings (not int).
        lcItemKey = loHttp.UrlDecode(loXml.GetChildContent("Contents[i]|Key"))
        loStringCompare.SetString(lcItemKey)
        lnStringMatch7 = loStringCompare.ContentsEqual("",1)
        IF ((loHttp.LastMethodSuccess <> 1) OR (lnStringMatch7 = 1)) THEN
            ? "Invalid object key."
            RELEASE loStringCompare
            RELEASE loCredentials
            RELEASE loHttp
            RELEASE loEndpoint
            RELEASE loBucketPath
            RELEASE loEncoded
            RELEASE loXml
            RELEASE loSeenTokens
            CANCEL
        ENDIF

        lcSize = loXml.GetChildContent("Contents[i]|Size")
        loStringCompare.SetString(lcSize)
        lnStringMatch8 = loStringCompare.ContentsEqual("",1)
        IF ((loXml.LastMethodSuccess <> 1) OR (lnStringMatch8 = 1)) THEN
            ? "Incomplete object entry."
            RELEASE loStringCompare
            RELEASE loCredentials
            RELEASE loHttp
            RELEASE loEndpoint
            RELEASE loBucketPath
            RELEASE loEncoded
            RELEASE loXml
            RELEASE loSeenTokens
            CANCEL
        ENDIF

        lcModified = loXml.GetChildContent("Contents[i]|LastModified")
        loStringCompare.SetString(lcModified)
        lnStringMatch9 = loStringCompare.ContentsEqual("",1)
        IF ((loXml.LastMethodSuccess <> 1) OR (lnStringMatch9 = 1)) THEN
            ? "Incomplete object entry."
            RELEASE loStringCompare
            RELEASE loCredentials
            RELEASE loHttp
            RELEASE loEndpoint
            RELEASE loBucketPath
            RELEASE loEncoded
            RELEASE loXml
            RELEASE loSeenTokens
            CANCEL
        ENDIF

        ? "Key: " + lcItemKey
        ? "Bytes: " + lcSize
        *  S3 returns ISO 8601, not RFC 822. Keep the original UTC timestamp.
        ? "Last modified (UTC): " + lcModified
        i = i + 1
    ENDDO
    lcTruncated = loXml.GetChildContent("IsTruncated")
    loStringCompare.SetString(lcTruncated)
    lnStringMatch10 = loStringCompare.ContentsEqual("true",1)
    loStringCompare.SetString(lcTruncated)
    lnStringMatch11 = loStringCompare.ContentsEqual("false",1)
    IF ((lnStringMatch10 <> 1) AND (lnStringMatch11 <> 1)) THEN
        ? "Missing or invalid IsTruncated; listing is incomplete."
        RELEASE loStringCompare
        RELEASE loCredentials
        RELEASE loHttp
        RELEASE loEndpoint
        RELEASE loBucketPath
        RELEASE loEncoded
        RELEASE loXml
        RELEASE loSeenTokens
        CANCEL
    ENDIF

    lnMore = 0
    loStringCompare.SetString(lcTruncated)
    lnStringMatch12 = loStringCompare.ContentsEqual("true",1)
    IF (lnStringMatch12 = 1) THEN
        lcToken = loXml.GetChildContent("NextContinuationToken")
        loStringCompare.SetString(lcToken)
        lnStringMatch13 = loStringCompare.ContentsEqual("",1)
        IF ((loXml.LastMethodSuccess <> 1) OR (lnStringMatch13 = 1)) THEN
            ? "Missing continuation token; listing is incomplete."
            RELEASE loStringCompare
            RELEASE loCredentials
            RELEASE loHttp
            RELEASE loEndpoint
            RELEASE loBucketPath
            RELEASE loEncoded
            RELEASE loXml
            RELEASE loSeenTokens
            CANCEL
        ENDIF

        IF (loSeenTokens.Contains(lcToken) = 1) THEN
            ? "Repeated continuation token; listing is incomplete."
            RELEASE loStringCompare
            RELEASE loCredentials
            RELEASE loHttp
            RELEASE loEndpoint
            RELEASE loBucketPath
            RELEASE loEncoded
            RELEASE loXml
            RELEASE loSeenTokens
            CANCEL
        ENDIF

        lnSuccess = loSeenTokens.AddStr(lcToken,"seen")
        IF (lnSuccess <> 1) THEN
            ? "Could not track pagination."
            RELEASE loStringCompare
            RELEASE loCredentials
            RELEASE loHttp
            RELEASE loEndpoint
            RELEASE loBucketPath
            RELEASE loEncoded
            RELEASE loXml
            RELEASE loSeenTokens
            CANCEL
        ENDIF

        lnMore = 1
    ENDIF

ENDDO
? "Object listing complete."

RELEASE loStringCompare
RELEASE loCredentials
RELEASE loHttp
RELEASE loEndpoint
RELEASE loBucketPath
RELEASE loEncoded
RELEASE loXml
RELEASE loSeenTokens