Reputation: 1306
i'm trying to get only the first file in a folder of a S3 Bucket.
Using the official PHP SDK, my code looks like:
$client = S3Client::factory(array('key'=>'...','secret'=>'...'));
$result = $client->getIterator('ListObjects',array(
'Bucket' => 'my_bucket_name',
'Prefix' => 'myfolder/',
'MaxKeys' => 1,
));
foreach($result as $object)
{
...
}
Now, looks like the MaxKeys parameter doesn't do anything, because this result contains all files in 'myfolder'
Reading the documentation again, seems like getIterator basically keeps running queries to AWS until grabs all the files from there, is there any way to really limit this to only one?
I've also tried running the query without getIterator, like this:
$result = $client->ListObjects(array(
'Bucket' => 'my_bucket_name',
'Prefix' => 'myfolder/',
'MaxKeys' => 1,
));
Which in this case i only get the Folder name but not the file, and also with a totally different format ,which i guess its the guzzle one:
Iterator Object
(
[storage:ArrayIterator:private] => Array
(
[Name] => my_bucket_name
[Prefix] => myfolder/
[Marker] => Array
(
)
[MaxKeys] => 1
[IsTruncated] => 1
[Contents] => Array
(
[0] => Array
(
[Key] => myfolder/
[LastModified] => 2014-02-03T13:17:55.000Z
[ETag] => "d41d8cd98f00b204e9800998ecf8427e"
[Size] => 0
[Owner] => Array
(
[ID] => ...
[DisplayName] => amazon
)
[StorageClass] => STANDARD
)
)
[EncodingType] =>
[RequestId] => E5TYHGG24FE73D8
)
)
How should i properly do this?
Thanks
Upvotes: 2
Views: 5793
Reputation: 6527
The 'MaxKeys'
parameter is applied to the operation, not the iterator, so you were actually doing a bunch of ListObjects
operations, that each returned one object, until all of the objects were returned.
Instead, you need to put a limit on the iterator as explained in the iterators section of the AWS SDK for PHP User Guide.
$iterator = $client->getListObjectsIterator(array(
'Bucket' => 'my-bucket'
), array(
'limit' => 1,
));
foreach ($iterator as $object) {
echo $object['Key'] . "\n";
}
// This should only print 1 object's key.
Also doing ->listObjects()
and getIterator('ListObjects')
do different things.
->listObjects()
executes a single S3 ListObjects operation and returns the full result as a Guzzle\Service\Resource\Model
, which is just an object that behaves like an array. See Modeled Responses.->getIterator('ListObjects')
returns an Aws\Common\Iterator\AwsResourceIterator
object, which implements PHP's Iterator
interface, and does nothing until you actually iterate over it (e.g., with foreach
). When you iterate over it, it emits data about each object one-by-one that was found in the response. It will make additional requests to S3 as needed until all objects matching the request parameters have been returned, or the specified limit
is reached.Upvotes: 4
Reputation: 360812
foreach ($result->getIterator() as $object) {
.... do stuff with $object;
break(); // terminate the loop
}
Upvotes: 0