gpt4 book ai didi

amazon-s3 - AWS PHP getIterator 不起作用 - 返回空结果

转载 作者:行者123 更新时间:2023-12-04 12:50:35 25 4
gpt4 key购买 nike

应该是简单的代码。我有 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);

我看到了:object(Generator)#95 (0) { }

我在第二天苦苦挣扎,找不到解决方案?有人知道我做错了什么吗?

最佳答案

我遇到了一个类似的问题,当对 getIterator 的结果使用 print_r 时,返回的内容似乎是空的:

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();}

关于amazon-s3 - AWS PHP getIterator 不起作用 - 返回空结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39436134/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com