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

S3: List Buckets with the Http Convenience Method

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.

Calls S3_ListBuckets and parses bucket names and creation timestamps without relying on Owner.DisplayName. Requires s3:ListAllMyBuckets.

Unpaginated helper: AWS recommends paginated ListBuckets requests. Unpaginated requests are supported only for accounts with the default general purpose bucket quota of 10,000; accounts with an approved higher quota must use pagination even if they currently own fewer buckets. This method exposes no pagination parameters, and AwsSubResources does not add them to its request in the tested runtime. Do not use this convenience example as a scalable inventory. A dedicated paginated ListBuckets example is recommended.

AWS reference

Chilkat PHP ActiveX Downloads

PHP ActiveX
<?php

$success = 0;

$stringCompare = new COM("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.
$credentials = new COM("Chilkat.JsonObject");
$credentials->Strict = 1;
$success = $credentials->LoadFile('qa_data/aws/s3.json');
if ($success != 1) {
    print 'Could not load private S3 credentials.' . "\n";
    exit;
}

$stringCompare->SetString($credentials->stringOf('access_key'));
$stringMatch1 = $stringCompare->ContentsEqual('',1);
if (($credentials->JsonTypeOf('access_key') != 1) or ($stringMatch1 == 1)) {
    print 'Missing or invalid access_key.' . "\n";
    exit;
}

$stringCompare->SetString($credentials->stringOf('secret_key'));
$stringMatch2 = $stringCompare->ContentsEqual('',1);
if (($credentials->JsonTypeOf('secret_key') != 1) or ($stringMatch2 == 1)) {
    print 'Missing or invalid secret_key.' . "\n";
    exit;
}

$http = new COM("Chilkat.Http");
$http->AwsAccessKey = $credentials->stringOf('access_key');
$http->AwsSecretKey = $credentials->stringOf('secret_key');
if ($credentials->HasMember('session_token') == 1) {
    $stringCompare->SetString($credentials->stringOf('session_token'));
    $stringMatch3 = $stringCompare->ContentsEqual('',1);
    if (($credentials->JsonTypeOf('session_token') != 1) or ($stringMatch3 == 1)) {
        print 'session_token must be a nonempty string when supplied.' . "\n";
        exit;
    }

    $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.
$region = 'us-west-2';
$http->AwsRegion = $region;
$endpoint = new COM("Chilkat.StringBuilder");
$endpoint->Append('s3.');
$endpoint->Append($region);
$endpoint->Append('.amazonaws.com');
$http->AwsEndpoint = $endpoint->getAsString();
$http->RequireSslCertVerify = 1;
$http->FollowRedirects = 0;
//  A region redirect is not success. Correct the endpoint and signing region.
//  This convenience method is UNPAGINATED. See the limitations below.
$xmlStr = $http->s3_ListBuckets();
if ($http->LastMethodSuccess != 1) {
    print 'Request failed (transport or S3 error). HTTP status: ' . $http->LastStatus . "\n";
    //  Inspect LastErrorText privately. Do not log credentials or signed URLs.
    exit;
}

if ($http->LastStatus != 200) {
    print 'Unexpected HTTP status: ' . $http->LastStatus . "\n";
    exit;
}

$xml = new COM("Chilkat.Xml");
$success = $xml->LoadXml($xmlStr);
if ($success != 1) {
    print 'Invalid XML response.' . "\n";
    exit;
}

$stringCompare->SetString($xml->Tag);
$stringMatch4 = $stringCompare->ContentsEqual('ListAllMyBucketsResult',1);
if ($stringMatch4 != 1) {
    print 'Unexpected XML response root.' . "\n";
    exit;
}

$i = 0;
$count = $xml->NumChildrenAt('Buckets');
while (($i < $count)) {
    $xml->I = $i;
    print 'Bucket: ' . $xml->getChildContent('Buckets|Bucket[i]|Name') . "\n";
    print 'Created (UTC): ' . $xml->getChildContent('Buckets|Bucket[i]|CreationDate') . "\n";
    //  Owner.DisplayName is not required or used.
    $i = $i + 1;
}

if ($xml->HasChildWithTag('ContinuationToken') == 1) {
    print 'More buckets exist. This unpaginated helper has not listed everything.' . "\n";
    exit;
}

print 'Bucket listing complete.' . "\n";

?>