2012-12-03 12 views
10

私は、S3上にファイルを格納しているクライアントのZipArchiveにS3バケットのコンテンツを取り込む方法を見つけようとしていますが、今度はS3でプッシュされたファイルを保持するレポートを作成する必要があります。顧客。私は(PEARでインストールされる)PHP SDK 2 APIで次のことを試してみました:S3からPHP SDK 2経由でObjectのコンテンツを取得しますか?

require 'AWSSDKforPHP/aws.phar'; 

use Aws\S3\S3Client; 
use Aws\Common\Enum\Region; 

$config = array(
    'key' => 'the-aws-key', 
    'secret' => 'the-aws-secret', 
    'region' => Region::US_EAST_1 
); 

$aws_s3 = S3Client::factory($config); 
$app_config['s3']['bucket'] = 'the-aws-bucket'; 
$app_config['s3']['prefix'] = ''; 
$attach_name = 'hosted-test-file.jpg'; 
try { 
    $result = $aws_s3->getObject(
     array(
      'Bucket' => $app_config['s3']['bucket'], 
      'Key' => $app_config['s3']['prefix'].$attach_name 
     ) 
    ); 
    var_dump($result); 
    $body = $result->get('Body'); 
    var_dump($body); 
    $handle = fopen('php://temp', 'r'); 
    $content = stream_get_contents($handle); 
    echo "String length: ".strlen($content); 
} catch(Aws\S3\Exception\S3Exception $e) { 
    echo "Request failed.<br />"; 
} 

しかし、それは返すすべてがそう、私はジップにそれをプッシュすることができ、実際のコンテンツをつかむ方法がわからGuzzle\Http\EntityBodyオブジェクトではありませんファイル。

Grabbing Object

object(Guzzle\Service\Resource\Model)[126] 
    protected 'structure' => object(Guzzle\Service\Description\Parameter)[109] 
    protected 'name' => null 
    protected 'description' => null 
    protected 'type' => string 'object' (length = 6) 
    protected 'required' => boolean false 
    protected 'enum' => null 
    protected 'additionalProperties' => boolean true 
    protected 'items' => null 
    protected 'parent' => null 
    protected 'ref' => null 
    protected 'format' => null 
    protected 'data' => array (size = 11) 
     'Body' => object(Guzzle\Http\EntityBody)[97] 
      protected 'contentEncoding' => boolean false 
      protected 'rewindFunction' => null 
      protected 'stream' => resource(292, stream) 
      protected 'size' => int 3078337 
      protected 'cache' => array (size = 9) 
      ... 
     'DeleteMarker' => string '' (length = 0) 
     'Expiration' => string '' (length = 0) 
     'WebsiteRedirectLocation' => string '' (length = 0) 
     'LastModified' => string 'Fri, 30 Nov 2012 21:07:30 GMT' (length = 29) 
     'ContentType' => string 'binary/octet-stream' (length = 19) 
     'ContentLength' => string '3078337' (length = 7) 
     'ETag' => string '"the-etag-of-the-file"' (length = 34) 
     'ServerSideEncryption' => string '' (length = 0) 
     'VersionId' => string '' (length = 0) 
     'RequestId' => string 'request-id' (length = 16) 

Returned from Body

object(Guzzle\Http\EntityBody)[96] 
    protected 'contentEncoding' => boolean false 
    protected 'rewindFunction' => null 
    protected 'stream' => resource(292, stream) 
    protected 'size' => int 3078337 
    protected 'cache' => array (size = 9) 
     'wrapper_type' => string 'php' (length = 3) 
     'stream_type' => string 'temp' (length = 4) 
     'mode' => string 'w+b' (length = 3) 
     'unread_bytes' => int 0 
     'seekable' => boolean true 
     'uri' => string 'php://temp' (length = 10) 
     'is_local' => boolean true 
     'is_readable' => boolean true 
     'is_writable' => boolean true 

// Echo of strlen() 
String length: 0 

すべての情報は、高いただければ幸いです、ありがとう!ソリューション

それは私

ながら、それを把握することが、私は、ファイルの内容を取得するためには、正しい方向に私を指摘要点を見つけることができたために次の操作を実行する必要があります:

require 'AWSSDKforPHP/aws.phar'; 

use Aws\S3\S3Client; 
use Aws\Common\Enum\Region; 

$config = array(
    'key' => 'the-aws-key', 
    'secret' => 'the-aws-secret', 
    'region' => Region::US_EAST_1 
); 

$aws_s3 = S3Client::factory($config); 
$app_config['s3']['bucket'] = 'the-aws-bucket'; 
$app_config['s3']['prefix'] = ''; 
$attach_name = 'hosted-test-file.jpg'; 
try { 
    $result = $aws_s3->getObject(
     array(
      'Bucket' => $app_config['s3']['bucket'], 
      'Key' => $app_config['s3']['prefix'].$attach_name 
     ) 
    ); 
    $body = $result->get('Body'); 
    $body->rewind(); 
    $content = $body->read($result['ContentLength']); 
} catch(Aws\S3\Exception\S3Exception $e) { 
    echo "Request failed.<br />"; 
} 
+0

本当に基本となるPHPストリームリソースをEntityBodyオブジェクトから取得する必要がある場合は、すべてのgetStream()メソッドを使用できます。 EntityBodyオブジェクトのAPIドキュメントについては、http://guzzlephp.org/api/class-Guzzle.Http.EntityBody.htmlを参照してください。 –

答えて

14

応答の本体はGuzzle\Http\EntityBodyオブジェクトに格納されています。これは、アプリケーションが非常に大きなファイルをダウンロードしたり、メモリ不足から保護するために使用されます。

あなたは文字列としてEntityBodyオブジェクトの内容を使用する必要がある場合は、あなたがオブジェクトを文字列にキャストすることができます。

$result = $s3Client->getObject(array(
    'Bucket' => $bucket, 
    'Key' => $key 
)); 

// Cast as a string 
$bodyAsString = (string) $result['Body']; 

// or call __toString directly 
$bodyAsString = $result['Body']->__toString(); 

必要な場合は、ターゲットファイルに直接ダウンロードすることができます。

0

getObjectを呼び出すと、オプションの配列を渡すことができます。これらのオプションでは、オブジェクトをファイルシステムにダウンロードするかどうかを指定できます。

$bucket = "bucketName"; 
$file = "fileName"; 
$downloadTo = "path/to/save"; 

$opts = array( // array of options 
    'fileDownload' => $downloadTo . $file // tells the SDK to download the 
              // file to this location 
); 

$result = $aws_s3->getObject($bucket, $file, $opts); 

getObject Reference

+0

私は最初のSDKであるPHP SDK 2を使用しています。適切なドキュメントは[こちら](http://docs.amazonwebservices.com/aws-sdk-php-2/latest/class-Aws.S3.S3Client.html)です。 – OpensaurusRex

+1

ああ、私の悪い。その場合は私の答えを無視してください。 SDK 2のドキュメントは、1.5のドキュメントに比べて悲しいことに欠けています。 – xbonez

+0

ええ、私は最終的には私がドキュメントLOLのすべてを試したので、答えのためにstackoverflowに向けることにしました。私は誰かがそれを理解したと確信しています。 :) – OpensaurusRex

0

私はバージョン2.00 SDKとその慣れていないけど、あなたはphp://tempにストリームコンテキストを通過してきたように見えます。あなたの更新の質問を見てからとドキュメントを簡単に一目から、ストリームが利用できると思われるよう:

$result = $aws_s3->getObject(
    array(
     'Bucket' => $app_config['s3']['bucket'], 
     'Key' => $app_config['s3']['prefix'].$attach_name 
    ) 
); 
$stream = $result->get('stream'); 
$content = file_get_contents($stream); 
+0

ああ、それはどういう意味ですか?私はそれを本当に素早くチェックさせてください。 – OpensaurusRex

+0

オブジェクトからの出力をさらに追加しました。 'binary/octet-stream'を返そうとしているようです。 – OpensaurusRex

+0

更新された質問とSDKのドキュメントを見て、私の答えを更新しました。ストリームハンドルは 'Guzzle \ Service \ Resource \ Model'オブジェクトとその内部に入れ子になった' Guzzle \ Http \ EntityBody'オブジェクトの両方で既に利用可能です。私の答えは、トップレベルのオブジェクトからストリームを取得することです。 –

0
<?php 
    $o_iter = $client->getIterator('ListObjects', array(
    'Bucket' => $bucketname 
    )); 
    foreach ($o_iter as $o) { 
    echo "{$o['Key']}\t{$o['Size']}\t{$o['LastModified']}\n"; 
    } 
+1

コードオンリーの回答は一般的に役立ちません。あなたが何をしているのか、理由を説明してください。 – MattDMo

+0

これは質問された質問とは関係ありません。 – Exit

関連する問題