Google Cloud Storage在bucket(PHP)中对对象进行分页



我想遍历bucket中的对象。我真的需要对此进行分页——我们在bucket中有成百上千个对象。我们的水桶看起来像:

bucket/MLS ID/file 1
bucket/MLS ID/file 2
bucket/MLS ID/file 3
... etc

下面是我代码的最简单版本。我知道我在$params['extToken']中设置的值是错误的,我不知道如何或在哪里获得正确的值$file_objects是"Google\Cloud\Storage\ObjectIterator",对吗?

// temp: pages of 10, out of a total of 100. I really want pages of 100
// out of all (in my test bucket, I have about 700 objects)
$params = [
'prefix'      => $mls_id,
'maxResults'  => 10,
'resultLimit' => 100,
'fields'      => 'items/id,items/name,items/updated,nextPageToken',
'pageToken'   => NULL
];
while ( $file_objects = $bucket->objects($params) )
{
foreach ( $file_objects as $object )
{
print "NAME: {$object->name()}n";
}
// I think that this might need to be encoded somehow?
// or how do I get the requested nextPageToken???
$params['pageToken'] = $file_objects->nextResultToken(); 
}

所以-我不理解maxResults和resultLimit。看起来resultLimit是我想从我的bucket中看到的总数,maxResults是我的页面大小。但maxResults似乎不会影响任何东西,而resultLimit会影响。

maxResults = 100
resultLimit = 10

生成10个对象。

maxResults = 10
resultLimit = 100

吐出100个物体。

maxResults = 10
resultLimit = 0

转储bucket中的所有702,而maxResults根本没有效果。并且在任何时候都不"$文件对象->nextResultToken(("给我任何东西。

我错过了什么?

objects方法会自动为您处理分页。它返回一个ObjectIterator对象。

resultLimit参数限制在所有页面上返回的对象总数。maxResults参数设置每页返回的最大数量。

如果在ObjectIterator对象上使用foreach,它将遍历所有对象,但请注意,ObjectIterator中还有其他方法,如iterateByPage

好吧,我想我明白了。我发现文档太稀疏,误导性太强。我想出的代码:

$params = [
'prefix' => <my prefix here>,
'maxResults' => 100,
//'resultLimit' => 0,
'fields' => 'items/id,items/name,items/updated,nextPageToken',
'pageToken' => NULL
];
// Note: setting 'resultLimit' to 0 does not work, I found the
//   docs misleading. If you want all results, don't set it at all
// Get the first set of objects per those parameters
$object_iterator = $bucket->objects($params);
// in order to get the next_result_token, I had to get the current 
//   object first. If you don't, nextResultToken() always returns 
//   NULL
$current = $object_iterator->current();
$next_result_token = $object_iterator->nextResultToken();
while ($next_result_token)
{
$object_page_iterator = $object_iterator->iterateByPage();
foreach ($object_page_iterator->current() as $file_object )
{
print " -- {$file_object->name()}n";
}
// here is where you use the page token retrieved earlier - get
//   a new set of objects
$params['pageToken'] = $next_result_token;
$object_iterator = $bucket->objects($params);
// Once again, get the current object before trying to get the
// next result token
$current = $object_iterator->current();
$next_result_token = $object_iterator->nextResultToken();
print "NEXT RESULT TOKEN: {$next_result_token}n";
}

这似乎对我有效,所以现在我可以解决实际问题了。希望这能帮助到别人。

最新更新