1
votes

Here is my code:

use Aws\S3\S3Client;  
use Aws\Exception\AwsException;

define('AWS_KEY', '****');
define('AWS_SECRET_KEY', '****');

// Instantiate the S3 class and point it at the desired host
$client = S3Client::factory(array(
'region' => 'us-east-1',
'version' => 'latest',
'endpoint' => "https://website.com",
'credentials' => [
    'key' => AWS_KEY,
    'secret' => AWS_SECRET_KEY
],
// Set the S3 class to use objects.dreamhost.com/bucket
// instead of bucket.objects.dreamhost.com
'use_path_style_endpoint' => true
));

$listResponse = $client->listBuckets();
print_r($listResponse);
$buckets = $listResponse['Buckets'];
foreach ($buckets as $bucket) {
    echo $bucket['Name'] . "\t" . $bucket['CreationDate'] . "\n";
}

And here is the response I get:

Aws\Result Object ( [data:Aws\Result:private] => Array ( [@metadata] => Array ( [statusCode] => 200 [effectiveUri] => https://website.com/ [headers] => Array ( [server] => nginx/1.16.1 [date] => Fri, 22 Jan 2021 04:57:56 GMT [content-type] => text/html; charset=UTF-8 [transfer-encoding] => chunked [connection] => keep-alive [x-xss-protection] => 1; mode=block [x-frame-options] => SAMEORIGIN [x-content-type-options] => nosniff [expect-ct] => enforce, max-age=300, report-uri='https://www.website.com' [x-cache] => BYPASS [strict-transport-security] => max-age=31536000 )

                [transferStats] => Array
                    (
                        [http] => Array
                            (
                                [0] => Array
                                    (
                                    )

                            )

                    )

            )

    )

[monitoringEvents:Aws\Result:private] => Array
    (
    )

)

I can't seem to get Buckets to appear (I currently have on bucket on Amazon S3).

Any suggestions as to why it won't show? Thanks ahead.

1
check access key policy whether it has permission to read or list bucket ? - aviboy2006
yes it seems to have access - Tony Friz
what exactly you are trying to do ? - aviboy2006
I'm just trying to list my buckets. - Tony Friz

1 Answers

3
votes

I am able to get list of bucket using below code. Only difference I have not used endpoint while creating S3 client object.

<?php
/**
 * Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
 *
 * This file is licensed under the Apache License, Version 2.0 (the "License").
 * You may not use this file except in compliance with the License. A copy of
 * the License is located at
 *
 * http://aws.amazon.com/apache2.0/
 *
 * This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
 * CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 *
 * ABOUT THIS PHP SAMPLE: This sample is part of the SDK for PHP Developer Guide topic at
 * https://docs.aws.amazon.com/sdk-for-php/v3/developer-guide/s3-examples-creating-buckets.html
 *
 */
// snippet-start:[s3.php.list_buckets.complete]
// snippet-start:[s3.php.list_buckets.import]

require 'vendor/autoload.php';

use Aws\S3\S3Client;  
use Aws\Exception\AwsException;
// snippet-end:[s3.php.list_buckets.import]


/**
 * List your Amazon S3 buckets.
 *
 * This code expects that you have AWS credentials set up per:
 * https://docs.aws.amazon.com/sdk-for-php/v3/developer-guide/guide_credentials.html
 */

//Create a S3Client 
// snippet-start:[s3.php.list_buckets.main]

define('AWS_KEY', '*****');
define('AWS_SECRET_KEY', '***************');

// Instantiate the S3 class and point it at the desired host
$s3Client = S3Client::factory(array('credentials' => [
    'key' => AWS_KEY,
    'secret' => AWS_SECRET_KEY
],'region' => 'us-east-1','version' => 'latest'));

//Listing all S3 Bucket
$buckets = $s3Client->listBuckets();
foreach ($buckets['Buckets'] as $bucket) {
    echo $bucket['Name'] . "\n";
}