我的驱动器上有这个层次结构:
-- logo1.png
|
|-- logo2.png
|
-- myFolder
| |
| --logo3.png
|
|
-- myPrivateFolder
|
--anotherPrivateFolder
|
-- logo4.png
我正在使用这个简单的脚本列出我的文件:
$client = new Google_Client();
$client->setAuthConfig($oauth_credentials);
$client->setRedirectUri($redirect_uri);
$client->addScope("https://www.googleapis.com/auth/drive");
$service = new Google_Service_Drive($client);
$result = array();
try {
$parameters = array(
'q' => "mimeType contains 'image/'",
);
$files = $service->files->listFiles($parameters);
$result = array_merge($result, $files->getFiles());
} catch (Exception $e) {
print "Une erreur s'est produite : " . $e->getMessage();
}
return $result;
作为参数,我在检索后提供了排除的文件夹ID:
$parameters = array('q' => "'not 'myFolderID' in parents and not 'myprivateFolderID' in parents");
运行我的脚本时,我只希望得到logo1.png
和logo2.png
,因为我排除了myFolder
和myPrivateFolder
,但我也得到了logo4.png
它有anotherPrivateFolder
而myPrivateFolder
是祖父母。我知道Google Drive API没有树结构的概念,因此我创建了一个递归函数,以便在排除的文件夹的所有子目录中传播。在这种情况下,我的脚本将获取myFolder
和myPrivateFolder
下面的所有文件夹的ID。这是我的功能:
$excluded_folders_id = array();
function retrieveAllChildren($service, $parameters) {
global $excluded_folders_id;
$results = array();
$pageToken = NULL;
do{
try {
if ($pageToken) {
$parameters['pageToken'] = $pageToken;
}
$files = $service->files->listFiles($parameters);
$results = array_merge($results, $files->getFiles());
$pageToken = $files->getNextPageToken();
} catch (Exception $e) {
print "Une erreur s'est produite : " . $e->getMessage();
$pageToken = NULL;
}
}while($pageToken);
foreach ($results as $result) {
echo "=> New children to exclude : ".$result->name."\n";
array_push($excluded_folders_id, $result->id);
retrieveAllChildren($service, array('q' => "'".$result->id."' in parents and mimeType = 'application/vnd.google-apps.folder'"));
}
}
retrieveAllChildren($service, array('q' => "name = 'myPrivateFolder'"))
因此,我有一个数组,其中包含我的排除文件夹及其所有子目录的ID。然后我构建查询,对于每个排除的文件夹ID,我添加...和'newsubdirectoryID'不在父项中。 在包含数百个子文件夹的驱动器上运行请求时,我遇到413错误,说我的客户端请求太大。如何排除文件夹及其所有内容,不受限制?
提前感谢您的任何帮助
编辑:
function isFileInExcluded($service, $entity_id){
global $excluded_folders_id;
$entityProperties = $service->files->get($entity_id,array('fields' => 'parents, id, name'));
If there is no more parents (so if on top of tree - no excluded encountered)
if($entityProperties->parents == NULL){
return true;
}
// If file has excluded folder on upper part of tree
if(in_array($entityProperties->parents[0], $excluded_folders_id) == true){
return true;
}else{
// Spread acrosse upper parents
$parentProperties = $service->files->get($entityProperties->parents[0],array('fields' => 'parents, id, name'));
isFileInExcluded($service, $parentProperties->id);
}
}
答案 0 :(得分:1)
"在包含数百个子文件夹的驱动器上运行请求时,我遇到413错误,说我的客户端请求太大"
您需要重新设计自己的应用。
您可以采取一些方法: -