应该是简单的代码。我有AWS SDK PHP v3,我创建了这样的代码:
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())
现在我看到了我的存储桶列表,但是当我尝试像这样得到ObjectList
getIterator
时:
$objects = $s3Client->getIterator('ListObjects', array(
'Bucket' => "bucket")
);
var_dump($objects);
我看到:对象(生成器)#95(0){}
我第二天正在努力,我无法找到解决方案? 谁知道我做错了什么?
答案 0 :(得分:1)
我遇到了一个类似的问题,当print_r
的结果使用getIterator
时,似乎返回的内容为空:
print_r($result);
Generator Object
(
)
使用var_dump:
var_dump($result);
object(Generator)#62 (0) {
}
我知道这个桶有结果,所以我很困惑为什么它总是显示0结果。这需要使用foreach
进行迭代,然后显示结果:
foreach ($result as $object) {
echo $object['Key']."\n";
}
有结果!
此外,您可以使用listObjectsV2
代替listObjects
来实现更现代的实现,并且可以使用PHP命令iterator_to_array
将结果转换为数组。
这是我的S3类的功能部分:
<?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();}