Note that there are some explanatory texts on larger screens.

plurals
  1. POGrabbing contents of Object from S3 via PHP SDK 2?
    primarykey
    data
    text
    <p>I have been trying to figure out how to grab contents from an S3 bucket to include in a ZipArchive for a client who is storing files on S3, they now need to create reports that hold the files that were pushed up to S3 by their customers. I have tried the following with the PHP SDK 2 API (Installed with PEAR):</p> <pre><code>require 'AWSSDKforPHP/aws.phar'; use Aws\S3\S3Client; use Aws\Common\Enum\Region; $config = array( 'key' =&gt; 'the-aws-key', 'secret' =&gt; 'the-aws-secret', 'region' =&gt; 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-&gt;getObject( array( 'Bucket' =&gt; $app_config['s3']['bucket'], 'Key' =&gt; $app_config['s3']['prefix'].$attach_name ) ); var_dump($result); $body = $result-&gt;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.&lt;br /&gt;"; } </code></pre> <p>However, all it returns is an <code>Guzzle\Http\EntityBody</code> object, not sure how to grab the actual content so I can push it into the zip file.</p> <p><a href="http://docs.amazonwebservices.com/aws-sdk-php-2/latest/class-Aws.S3.S3Client.html#_getObject" rel="noreferrer">Grabbing Object</a></p> <pre><code>object(Guzzle\Service\Resource\Model)[126] protected 'structure' =&gt; object(Guzzle\Service\Description\Parameter)[109] protected 'name' =&gt; null protected 'description' =&gt; null protected 'type' =&gt; string 'object' (length = 6) protected 'required' =&gt; boolean false protected 'enum' =&gt; null protected 'additionalProperties' =&gt; boolean true protected 'items' =&gt; null protected 'parent' =&gt; null protected 'ref' =&gt; null protected 'format' =&gt; null protected 'data' =&gt; array (size = 11) 'Body' =&gt; object(Guzzle\Http\EntityBody)[97] protected 'contentEncoding' =&gt; boolean false protected 'rewindFunction' =&gt; null protected 'stream' =&gt; resource(292, stream) protected 'size' =&gt; int 3078337 protected 'cache' =&gt; array (size = 9) ... 'DeleteMarker' =&gt; string '' (length = 0) 'Expiration' =&gt; string '' (length = 0) 'WebsiteRedirectLocation' =&gt; string '' (length = 0) 'LastModified' =&gt; string 'Fri, 30 Nov 2012 21:07:30 GMT' (length = 29) 'ContentType' =&gt; string 'binary/octet-stream' (length = 19) 'ContentLength' =&gt; string '3078337' (length = 7) 'ETag' =&gt; string '"the-etag-of-the-file"' (length = 34) 'ServerSideEncryption' =&gt; string '' (length = 0) 'VersionId' =&gt; string '' (length = 0) 'RequestId' =&gt; string 'request-id' (length = 16) </code></pre> <p><a href="http://docs.amazonwebservices.com/aws-sdk-php-2/latest/class-Guzzle.Http.EntityBody.html" rel="noreferrer">Returned from Body</a></p> <pre><code>object(Guzzle\Http\EntityBody)[96] protected 'contentEncoding' =&gt; boolean false protected 'rewindFunction' =&gt; null protected 'stream' =&gt; resource(292, stream) protected 'size' =&gt; int 3078337 protected 'cache' =&gt; array (size = 9) 'wrapper_type' =&gt; string 'php' (length = 3) 'stream_type' =&gt; string 'temp' (length = 4) 'mode' =&gt; string 'w+b' (length = 3) 'unread_bytes' =&gt; int 0 'seekable' =&gt; boolean true 'uri' =&gt; string 'php://temp' (length = 10) 'is_local' =&gt; boolean true 'is_readable' =&gt; boolean true 'is_writable' =&gt; boolean true // Echo of strlen() String length: 0 </code></pre> <p>Any information would be high appreciated, thanks!</p> <p><strong>Solution</strong></p> <p>It me a while to figure it out but I was able to find a gist that pointed me in the right direction, in order to get the contents of the file you need to do the following:</p> <pre><code>require 'AWSSDKforPHP/aws.phar'; use Aws\S3\S3Client; use Aws\Common\Enum\Region; $config = array( 'key' =&gt; 'the-aws-key', 'secret' =&gt; 'the-aws-secret', 'region' =&gt; 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-&gt;getObject( array( 'Bucket' =&gt; $app_config['s3']['bucket'], 'Key' =&gt; $app_config['s3']['prefix'].$attach_name ) ); $body = $result-&gt;get('Body'); $body-&gt;rewind(); $content = $body-&gt;read($result['ContentLength']); } catch(Aws\S3\Exception\S3Exception $e) { echo "Request failed.&lt;br /&gt;"; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload