2016-09-11 2 views
2

간단한 코드 여야합니다. 나는 AWS SDK의 PHP의 V3를하고 난 같은 코드 생성 :AWS PHP getIterator가 작동하지 않습니다. 빈 결과를 반환합니다.

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

지금 나는 나의 버킷리스트를 참조하지만,이 같은 ObjectListgetIterator 얻을 시도 할 때 : (개체 : 내가 볼

$objects = $s3Client->getIterator('ListObjects', array(
    'Bucket' => "bucket") 
); 

var_dump($objects); 

을 생성기) # 95 (0) {}

나는이 두 번째 날에 어려움을 겪고 있으며 해결책을 찾을 수 없습니까? 내가 뭘 잘못했는지 아는 사람이 있습니까?

print_r($result); 

Generator Object 
(
) 

와 위해서 var_dump로 : 내가 아는

var_dump($result); 

object(Generator)#62 (0) { 
} 

getIterator의 결과에 print_r를 사용하는 경우

+0

확인 ($ 결과 = $ s3Client->하고 ListObjects를 ([ '버킷'=> '내 버킷'])) ; 나는 2 apis를 혼합했다, 나는 절름발이 다. – Wraith

답변

1

내가 반환 된 내용처럼 보였다하는 유사한 문제에 달렸다는 비어 있었다 양동이에 대한 결과가 있었기 때문에 항상 결과가 0 인 이유가 혼란 스러웠습니다. 이것은 말 그대로 foreach를 사용하여 반복해야하고 결과는 공개됩니다

foreach ($result as $object) { 
echo $object['Key']."\n"; 
} 

는 결과가 있습니다!

listObjects 대신 listObjectsV2을 사용하면 최신 구현을 위해 PHP 명령어 iterator_to_array을 사용하여 결과를 배열로 변환 할 수 있습니다. 여기

내 S3 클래스의 기능적 부분이다 : 그것을 \t \t에서 print_r fixedd

<?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();} 
관련 문제