Skip to content

Instantly share code, notes, and snippets.

@chapdel
Forked from harshavardhana/aws-sdk-example.php
Created July 29, 2021 13:01
Show Gist options
  • Save chapdel/4a40308942d14695d7f827bac7f6f46f to your computer and use it in GitHub Desktop.
Save chapdel/4a40308942d14695d7f827bac7f6f46f to your computer and use it in GitHub Desktop.

Revisions

  1. Harshavardhana created this gist Feb 2, 2016.
    38 changes: 38 additions & 0 deletions aws-sdk-example.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,38 @@
    <?php
    // Include the SDK using the Composer autoloader
    date_default_timezone_set('America/Los_Angeles');
    require 'vendor/autoload.php';

    $s3 = new Aws\S3\S3Client([
    'version' => 'latest',
    'region' => 'us-east-1',
    'endpoint' => 'http://localhost:9000'
    ]);

    $result = $s3->listBuckets();
    echo "ListBuckets: \n";
    foreach ($result['Buckets'] as $bucket) {
    echo $bucket['Name'] . " " . $bucket['CreationDate'] . "\n";
    }

    echo "ListObjects: \n";
    $results = $s3->getPaginator('ListObjects', [
    'Bucket' => 'testbucket'
    ]);

    foreach ($results as $result) {
    foreach ($result['Contents'] as $object) {
    echo $object['Key'] . " " . $object['Size'] . "\n";
    }
    }

    echo "ListObjects: delimited\n";
    $results = $s3->getPaginator('ListObjects', [
    'Bucket' => 'testbucket',
    'Delimiter' => '/'
    ]);

    $expression = '[CommonPrefixes[].Prefix, Contents[].Key][]';
    foreach ($results->search($expression) as $item) {
    echo $item . "\n";
    }