How to get an array of keys from an amazon s3 object

I am using amazon s3 v3 php sdk and I am trying to get the key of the whole object for this, I am using

s3->listObjects([ 'Bucket' => $somebucketName]); 

this function works fine and I get the whole object under the bucket $somebucketName and its in the form below

 Aws\Result Object ( [data:Aws\Result:private] => Array ( [IsTruncated] => [Marker] => [Contents] => Array ( [0] => Array ( [Key] => 1.PNG [LastModified] => Aws\Api\DateTimeResult Object ( [date] => 2015-07-14 07:22:25.000000 [timezone_type] => 2 [timezone] => Z ) [ETag] => "23f423234v23v42424d23" [Size] => 19980 [StorageClass] => STANDARD [Owner] => Array ( [DisplayName] => sfsfssfsdf [ID] => 242f2342242342252g42f42vt34 ) ) [1] => Array ( [Key] => 58.jpg [LastModified] => Aws\Api\DateTimeResult Object ( [date] => 2015-07-14 07:20:26.000000 [timezone_type] => 2 [timezone] => Z ) [ETag] => "vrtet4v4t54tvt4gvtgv45" [Size] => 1226694 [StorageClass] => STANDARD [Owner] => Array ( [DisplayName] => sfsfssfsdf [ID] => 34t3t3t3y43y4yg5yy4vg6u676 ) ) [2] => Array ( [Key] => HDFHDFHDFHDFHFHFH [LastModified] => Aws\Api\DateTimeResult Object ( [date] => 2015-07-30 12:07:42.000000 [timezone_type] => 2 [timezone] => Z ) [ETag] => "3453345343rcf3f3r3r3f" ) [Name] => SFSSD [Prefix] => [MaxKeys] => 1000 [@metadata] => Array ( [statusCode] => 200 [effectiveUri] => https://s3-us-west-2.amazonaws.com/SFSSD [headers] => Array ( [x-amz-id-2] => sdfsfs234sfs [x-amz-request-id] => HSJFSD899 [date] => Mon, 03 Aug 2015 06:46:48 GMT [x-amz-bucket-region] => us-west-2 [content-type] => application/xml [transfer-encoding] => chunked [server] => AmazonS3 ) ) ) ) 

now my question is how to get an array of keys as below object

 array("1.png","58.jpg","HDDHDFHDHDGH); 
+4
source share
2 answers

Aws\Result implements ArrayAccess . You can access the content as follows:

 $result = $s3->listObjects(['Bucket' => $somebucketName]) $contents = $result['Contents']; 
+3
source

From comment : Please note that v1 of the SDK is deprecated . If you are still using it, you can use the following snippet:

 $ObjectsListResponse = s3->list_objects([ 'Bucket' => $somebucketName]); $Objects = $ObjectsListResponse->body->Contents; foreach ($Objects as $Object) { $keyArray[] = $Object->Key; } 

PHP S3 Examples

+1
source

All Articles