Should be simple code. I have AWS SDK PHP v3 and I created code like this:
require 'aws/aws-autoloader.php';
use Aws\S3\S3Client;
$s3Client = new S3Client([
'version' => 'latest',
'region' => 'eu-west-1',
'credentials' => [
'key' => "accesskey",
'secret' => "secretkey",
],
]);
print_r($result = $s3Client->listBuckets())
Now I see my buckets list, but when I tried get ObjectList getIterator like this:
$objects = $s3Client->getIterator('ListObjects', array(
'Bucket' => "bucket")
);
var_dump($objects);
I see: object(Generator)#95 (0) { }
I am struggling with this second day and I can't find solution? Anyone knows what I do wrong?
I ran in to a similar issue in which is seemed like the returned contents were empty when using print_r on the results of getIterator:
print_r($result);
Generator Object
(
)
And with var_dump:
var_dump($result);
object(Generator)#62 (0) {
}
I knew there were results for the bucket, so I was confused why it was always showing 0 results. This literally needs to be iterated by using foreach, and then the results are revealed:
foreach ($result as $object) {
echo $object['Key']."\n";
}
There are the results!
Also, you can use listObjectsV2 instead of listObjects for the more modern implementation, and you can use the PHP command iterator_to_array to convert the results in to an array.
Here is a functional portion of my S3 class:
<?php
require_once('aws.phar');
class S3 {
private $region = 'us-east-1';
private $s3;
public function create() {
try {
$this->s3 = Aws\S3\S3Client::factory(array(
'credentials' => array(
'key' => AWS_ACCESS_KEY_ID,
'secret' => AWS_SECRET_ACCESS_KEY),
'region' => $this->region,
'version' => 'latest'
));
return ($this->s3 ? true : false);
}
catch (Aws\Exception\AwsException $e) {throw new Exception($e->getMessage());}
catch (Aws\S3\Exception\S3Exception $e) {throw new Exception($e->getAwsErrorType().' '.$e->getAwsErrorCode());}
catch (Exception $e) {throw new Exception($e->getMessage());}
return false;
}
public function listObjects($bucket,$prefix = '',$start_after = '',$max_keys = '') {
if (!$this->checkActive()) return false;
$args = array('Bucket' => $bucket,'EncodingType' => 'url');
if ($max_keys > 0) $args['MaxKeys'] = intval($max_keys);
if ($prefix) $args['Prefix'] = $prefix;
if ($start_after) $args['StartAfter'] = $start_after;
try {
$response = $this->s3->ListObjectsV2($args);
if (!$response) return false;
$data = $response->toArray();
return (isset($data['Contents']) ? $data['Contents'] : array());
}
catch (Aws\Exception\AwsException $e) {throw new Exception ($e->getMessage());}
catch (Aws\S3\Exception\S3Exception $e) {throw new Exeception ($e->getAwsRequestId().' : '.$e->getAwsErrorType().' : '.$e->getAwsErrorCode());}
catch (Exception $e) {throw new Exception ($e->getMessage());}
}
public function listAllObjects($bucket,$prefix = '') {
if (!$this->checkActive()) return false;
$args = array('Bucket' => $bucket,'EncodingType' => 'url');
if ($prefix) $args['Prefix'] = $prefix;
try {
$response = $this->s3->getIterator('ListObjectsV2',$args,$args);
if (!$response) return false;
return iterator_to_array($response);
}
catch (Aws\Exception\AwsException $e) {throw new Exception ($e->getMessage());}
catch (Aws\S3\Exception\S3Exception $e) {throw new Exeception ($e->getAwsRequestId().' : '.$e->getAwsErrorType().' : '.$e->getAwsErrorCode());}
catch (Exception $e) {throw new Exception ($e->getMessage());}
}
private function checkActive() {
if ($this->s3) return true;
return $this->create();
}
}
try {
$s3 = new S3();
$list = $s3->listObjects($bucket,$key_starts_with,$full_key_to_start_after,$number_to_return);
print_r($list);
$list = $s3->listAllObjects($bucket,$key_starts_with);
print_r($list);
}
catch (Exception $e) {echo 'Error: '.$e->getMessage();}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With