List objects from my Buckets - Amazon S3 SDK PHP

Asked

Viewed 725 times

1

My project is:

  • List my Buckets

  • List the objects of each Bucket

I created a foreach to list the Buckets and it worked perfectly.

The same thing didn’t happen with the foreach of the objects.

$buckets = $s3->getIterator('ListBuckets', []);
foreach($buckets as $bucket) {

    $objects = $s3->getIterator('ListObjects', [ 'Bucket' => $bucket ]);
    foreach($objects as $object) {
        // não retornou nada e ainda deu erro
    }

}

Error in page displays:

Fatal error: Uncaught InvalidArgumentException: Found 1 error while validating the input provided for the ListObjects operation: [Bucket] must be a string or an object that implements __toString(). Found array(2) in C:\xampp\htdocs\backup\vendor\aws\aws-sdk-php\src\Api\Validator.php:65 Stack trace: #0 C:\xampp\htdocs\backup\vendor\aws\aws-sdk-php\src\Middleware.php(79): Aws\Api\Validator->validate('ListObjects', Object(Aws\Api\StructureShape), Array) #1 C:\xampp\htdocs\backup\vendor\aws\aws-sdk-php\src\S3\S3Client.php(405): Aws\Middleware::Aws\{closure}(Object(Aws\Command), NULL) #2 C:\xampp\htdocs\backup\vendor\aws\aws-sdk-php\src\S3\S3Client.php(428): Aws\S3\S3Client::Aws\S3\{closure}(Object(Aws\Command), NULL) #3 C:\xampp\htdocs\backup\vendor\aws\aws-sdk-php\src\S3\S3Client.php(362): Aws\S3\S3Client::Aws\S3\{closure}(Object(Aws\Command), NULL) #4 C:\xampp\htdocs\backup\vendor\aws\aws-sdk-php\src\S3\S3Client.php(381): Aws\S3\S3Client::Aws\S3\{closure}(Object(Aws\Command), NULL) #5 C:\xampp\htdocs\backup\vendor\aws\aws-s in C:\xampp\htdocs\backup\vendor\aws\aws-sdk-php\src\Api\Validator.php on line 65

Would anyone know how to make this listing of objects inside the Buckets work?

Any help is welcome!

  • 1

    Welcome to Stackoverflow in Portuguese. The official language used here is Portuguese, can you translate your question? If you prefer, you can ask the same question on Stackoverflow.com.

1 answer

1


Your problem is that the method ListBuckets returns a multi-dimensional array with the Buckets data.

Try replacing the following line:

$objects = $s3->getIterator('ListObjects', [ 'Bucket' => $bucket ]);

for this

$objects = $s3->getIterator('ListObjects', [ 'Bucket' => $bucket['name'] ]);

If it hasn’t worked yet, show the command output var_dump($bucket).

  • I tried with $Bucket['Name'] and it didn’t work either. One thing I did now was create another user to test a new access and managed to list the objects quietly.... It seems that it has something to do with some kind of restriction on the other user’s credentials...

  • Could you tell which error you have presented now? If possible, also paste the output of the var_dump($Buket)

  • var_dump($Bucket): array(2) { ["Name"]=> string(12) "SATLCARRETAO" ["CreationDate"]=> object(Aws\Api\DateTimeResult)#110 (3) { ["date"]=> string(26) "2016-10-17 11:48:32.000000" ["timezone_type"]=> int(2) ["timezone"]=> string(1) "Z" } } error: Fatal error: Uncaught Aws\S3\Exception\PermanentRedirectException: Encountered a permanent redirect while requesting https://s3-sa-east-1.amazonaws.com/SATLCARRETAO?encoding-type=url. Are you sure you are using the correct region for this bucket?

  • I used as reference the only Bucket that I KNOW I have access and managed to list the Objects without problems, I will have to check this credentials situation, I got a problem from the :-D project thanks for the help!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.