2012-12-03 11 views
10

मैं एस 3 बाल्टी से सामग्री को पकड़ने के लिए एस 3 पर फ़ाइलों को संग्रहीत करने वाले क्लाइंट के लिए एक ZipArchive में शामिल करने के तरीके को समझने की कोशिश कर रहा हूं, अब उन्हें उन रिपोर्टों को बनाने की आवश्यकता है जो उन फ़ाइलों को पकड़ते हैं जिन्हें S3 तक धक्का दिया गया था ग्राहकों।PHP एसडीके 2 के माध्यम से एस 3 से ऑब्जेक्ट की सामग्री को पकड़ना?

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 वस्तु, नहीं यकीन है कि कैसे वास्तविक सामग्री हड़पने के लिए तो मैं यह ज़िप में धक्का कर सकते हैं: मैं PHP एसडीके 2 एपीआई (नाशपाती के साथ स्थापित) के साथ निम्नलिखित की कोशिश की है फ़ाइल।

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

तुम सच में एक EntityBody वस्तु से अंतर्निहित पीएचपी धारा संसाधन हड़पने के लिए की जरूरत है, आप कर सकते हैं सब 'getStream()' विधि। EntityBody ऑब्जेक्ट के एपीआई दस्तावेज़ों के लिए http://guzzlephp.org/api/class-Guzzle.Http.EntityBody.html देखें। –

उत्तर

14

प्रतिक्रिया का ढाँचा एक Guzzle\Http\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(); 

आवश्यकता होने पर आप लक्ष्य फ़ाइल के लिए सीधे डाउनलोड कर सकते हैं:

आप एक स्ट्रिंग के रूप EntityBody ऑब्जेक्ट की सामग्री का उपयोग करने की जरूरत है, तो आप एक स्ट्रिंग के लिए वस्तु डाल सकता

use Guzzle\Http\EntityBody; 

$s3Client->getObject(array(
    'Bucket' => $bucket, 
    'Key' => $key, 
    'command.response_body' => EntityBody::factory(fopen("/tmp/{$key}", 'w+')) 
)); 
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

मैं PHP एसडीके 2 का उपयोग कर रहा हूं, यह पहला एसडीके है। उपयुक्त दस्तावेज़ [यहां] हैं (http://docs.amazonwebservices.com/aws-sdk-php-2/latest/class-Aws.S3.S3Client.html)। – OpensaurusRex

+1

आह, मेरा बुरा। उस मामले में मेरा जवाब अनदेखा करें। एसडीके 2 के दस्तावेज़ों में 1.5 के दस्तावेज़ों की तुलना में दुख की कमी है। – xbonez

+0

हां, यही कारण है कि मैंने आखिरकार जवाब के लिए स्टैक ओवरफ्लो पर जाने का फैसला किया क्योंकि मैंने दस्तावेज़ों में सब कुछ करने की कोशिश की है। मुझे यकीन है कि किसी ने इसे समझ लिया है। :) – OpensaurusRex

0

मुझे लगता है कि संस्करण 2.00 एसडीके से परिचित नहीं हूँ, लेकिन जैसे आप 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

मैंने ऑब्जेक्ट्स से अधिक आउटपुट जोड़ा, ऐसा लगता है कि यह 'बाइनरी/ऑक्टेट-स्ट्रीम' वापस करने की कोशिश कर रहा है। – OpensaurusRex

+0

मैंने आपके अपडेट किए गए प्रश्न और एसडीके दस्तावेज़ों को देखा और मेरा जवाब अपडेट कर दिया है। ऐसा लगता है कि स्ट्रीम हैंडल पहले से ही '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

संबंधित मुद्दे