Debashis Dash
Debashis Dash

Reputation: 342

AWS S3 getObject is not able to read object content through PHP SDK

I have tried to read content from a s3 object through the below code.

$content = $s3Client->getObject(
                        array(
                            'Bucket'=> $bucketName,
                            'Key' =>  $pathToObject,
                            'ResponseContentType' => 'text/plain',
                        )
                    );

And I got below response

GuzzleHttp\Psr7\Stream Object ( [stream:GuzzleHttp\Psr7\Stream:private] => Resource id #87 [size:GuzzleHttp\Psr7\Stream:private] => [seekable:GuzzleHttp\Psr7\Stream:private] => 1 [readable:GuzzleHttp\Psr7\Stream:private] => 1 [writable:GuzzleHttp\Psr7\Stream:private] => 1 [uri:GuzzleHttp\Psr7\Stream:private] => php://temp [customMetadata:GuzzleHttp\Psr7\Stream:private] => Array ( )

)

Any help will be appreciated to read object content in S3.

Upvotes: 3

Views: 4224

Answers (1)

Abbas
Abbas

Reputation: 560

Actually its return Psr7\Stream object.

So if we need to get contents from PSR Stream we have to call getContents() method from the object.

<?php

$s3Client = new Aws\S3\S3Client(array(
    'stats'   => TRUE,
    'http'    => array(
        'verify' => FALSE,
        'connect_timeout' => 30
    ),
    'version'     => 'latest'
));     

$result = $s3Client->getObject(array(
    'Key'    => $filename,
    'Bucket' => $bucketName
));

echo $result['Body']->getContents();
//Also you can get metadata like this print_r($result['Body']->getMetadata());

Hope this will help someone who is actually using SDK version 3.

Specification here https://docs.aws.amazon.com/aws-sdk-php/v3/api/class-GuzzleHttp.Psr7.Stream.html

Upvotes: 8

Related Questions