[英]AWS PHP SDK getIterator() and MaxKeys
i'm trying to get only the first file in a folder of a S3 Bucket. 我正在尝试仅在S3存储桶的文件夹中获取第一个文件。
Using the official PHP SDK, my code looks like: 使用官方的PHP SDK,我的代码如下所示:
$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' 现在,看起来MaxKeys参数没有执行任何操作,因为此结果包含“ 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? 再次阅读文档,似乎getIterator基本上一直在向AWS运行查询,直到从那里获取所有文件为止,有没有办法将其真正限制为仅一个?
I've also tried running the query without getIterator, like this: 我也尝试过在没有getIterator的情况下运行查询,如下所示:
$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 谢谢
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. 'MaxKeys'
参数应用于该操作,而不是迭代器,因此您实际上是在执行一堆ListObjects
操作,每个操作返回一个对象,直到所有对象都被返回。
Instead, you need to put a limit on the iterator as explained in the iterators section of the AWS SDK for PHP User Guide . 相反,您需要对迭代器进行限制,如《 AWS SDK for PHP用户指南》的迭代器部分所述。
$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()
和getIterator('ListObjects')
做不同的事情。
->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. ->listObjects()
执行一个S3 ListObjects操作,并以Guzzle\\Service\\Resource\\Model
的Guzzle\\Service\\Resource\\Model
返回完整结果,这只是一个行为类似于数组的对象。 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 (eg, with foreach
). ->getIterator('ListObjects')
返回一个Aws\\Common\\Iterator\\AwsResourceIterator
对象,该对象实现PHP的Iterator
接口 ,在您实际对其进行迭代之前(例如,使用foreach
),它什么也不做。 When you iterate over it, it emits data about each object one-by-one that was found in the response. limit
is reached. limit
。 foreach ($result->getIterator() as $object) {
.... do stuff with $object;
break(); // terminate the loop
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.