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

S3: Connect to an S3-Compatible Provider

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-compatible.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 separate provider credentials in qa_data/aws/s3-compatible.json; do not send AWS credentials to an unrelated endpoint. Replace the reserved example hostname and signing region with the provider's documented values. The endpoint is a hostname without an https:// prefix. SigV4 remains the default and TLS verification stays enabled.

S3 compatibility is not identical to AWS S3. Verify authentication, addressing style, supported headers, pagination, versioning, checksums, and error semantics with the provider. This example calls the unpaginated bucket helper and displays the validated XML response; it does not claim that every provider has the same quota or pagination behavior as AWS.

AWS reference

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkHttp.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkJsonObject.pb"
IncludeFile "CkXml.pb"

Procedure ChilkatExample()

    success.i = 0

    stringCompare.i = CkStringBuilder::ckCreate()
    If stringCompare.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ;  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.
    credentials.i = CkJsonObject::ckCreate()
    If credentials.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::setCkStrict(credentials, 1)
    success = CkJsonObject::ckLoadFile(credentials,"qa_data/aws/s3-compatible.json")
    If success <> 1
        Debug "Could not load private S3 credentials."
        CkStringBuilder::ckDispose(stringCompare)
        CkJsonObject::ckDispose(credentials)
        ProcedureReturn
    EndIf

    CkStringBuilder::ckSetString(stringCompare,CkJsonObject::ckStringOf(credentials,"access_key"))
    stringMatch1.i = CkStringBuilder::ckContentsEqual(stringCompare,"",1)
    If (CkJsonObject::ckJsonTypeOf(credentials,"access_key") <> 1) OR (stringMatch1 = 1)
        Debug "Missing or invalid access_key."
        CkStringBuilder::ckDispose(stringCompare)
        CkJsonObject::ckDispose(credentials)
        ProcedureReturn
    EndIf

    CkStringBuilder::ckSetString(stringCompare,CkJsonObject::ckStringOf(credentials,"secret_key"))
    stringMatch2.i = CkStringBuilder::ckContentsEqual(stringCompare,"",1)
    If (CkJsonObject::ckJsonTypeOf(credentials,"secret_key") <> 1) OR (stringMatch2 = 1)
        Debug "Missing or invalid secret_key."
        CkStringBuilder::ckDispose(stringCompare)
        CkJsonObject::ckDispose(credentials)
        ProcedureReturn
    EndIf

    http.i = CkHttp::ckCreate()
    If http.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkHttp::setCkAwsAccessKey(http, CkJsonObject::ckStringOf(credentials,"access_key"))
    CkHttp::setCkAwsSecretKey(http, CkJsonObject::ckStringOf(credentials,"secret_key"))
    If CkJsonObject::ckHasMember(credentials,"session_token") = 1
        CkStringBuilder::ckSetString(stringCompare,CkJsonObject::ckStringOf(credentials,"session_token"))
        stringMatch3.i = CkStringBuilder::ckContentsEqual(stringCompare,"",1)
        If (CkJsonObject::ckJsonTypeOf(credentials,"session_token") <> 1) OR (stringMatch3 = 1)
            Debug "session_token must be a nonempty string when supplied."
            CkStringBuilder::ckDispose(stringCompare)
            CkJsonObject::ckDispose(credentials)
            CkHttp::ckDispose(http)
            ProcedureReturn
        EndIf

        CkHttp::setCkAwsSessionToken(http, CkJsonObject::ckStringOf(credentials,"session_token"))
    EndIf

    ;  Use the provider's documented signing region and TLS endpoint.
    ;  This reserved example endpoint must be replaced before running.
    CkHttp::setCkAwsRegion(http, "us-east-1")
    CkHttp::setCkAwsEndpoint(http, "objects.example.com")
    CkHttp::setCkRequireSslCertVerify(http, 1)
    CkHttp::setCkFollowRedirects(http, 0)
    xmlStr.s = CkHttp::ckS3_ListBuckets(http)
    If CkHttp::ckLastMethodSuccess(http) <> 1
        Debug "Request failed (transport or S3 error). HTTP status: " + Str(CkHttp::ckLastStatus(http))
        ;  Inspect LastErrorText privately. Do not log credentials or signed URLs.
        CkStringBuilder::ckDispose(stringCompare)
        CkJsonObject::ckDispose(credentials)
        CkHttp::ckDispose(http)
        ProcedureReturn
    EndIf

    If CkHttp::ckLastStatus(http) <> 200
        Debug "Unexpected HTTP status: " + Str(CkHttp::ckLastStatus(http))
        CkStringBuilder::ckDispose(stringCompare)
        CkJsonObject::ckDispose(credentials)
        CkHttp::ckDispose(http)
        ProcedureReturn
    EndIf

    xml.i = CkXml::ckCreate()
    If xml.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkXml::ckLoadXml(xml,xmlStr)
    If success <> 1
        Debug "Invalid XML response."
        CkStringBuilder::ckDispose(stringCompare)
        CkJsonObject::ckDispose(credentials)
        CkHttp::ckDispose(http)
        CkXml::ckDispose(xml)
        ProcedureReturn
    EndIf

    CkStringBuilder::ckSetString(stringCompare,CkXml::ckTag(xml))
    stringMatch4.i = CkStringBuilder::ckContentsEqual(stringCompare,"ListAllMyBucketsResult",1)
    If stringMatch4 <> 1
        Debug "Unexpected XML response root."
        CkStringBuilder::ckDispose(stringCompare)
        CkJsonObject::ckDispose(credentials)
        CkHttp::ckDispose(http)
        CkXml::ckDispose(xml)
        ProcedureReturn
    EndIf

    Debug CkXml::ckGetXml(xml)
    Debug "Provider bucket response received; apply its documented pagination rules."


    CkStringBuilder::ckDispose(stringCompare)
    CkJsonObject::ckDispose(credentials)
    CkHttp::ckDispose(http)
    CkXml::ckDispose(xml)


    ProcedureReturn
EndProcedure