I have this hierarchy on my drive :
-- logo1.png
|
|-- logo2.png
|
-- myFolder
| |
| --logo3.png
|
|
-- myPrivateFolder
|
--anotherPrivateFolder
|
-- logo4.png
I'm using this simple script to list my files :
$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;
As a parameter, I give excluded folders ID after retrieving them :
$parameters = array('q' => "'not 'myFolderID' in parents and not 'myprivateFolderID' in parents");
When running my script, I only expect to get logo1.png
and logo2.png
, since I excluded myFolder
and myPrivateFolder
, but I also get logo4.png
since it has anotherPrivateFolder
and myPrivateFolder
is a grandparent. I understood that Google Drive API has no notion of tree structure, so I made a recursive function to spread along all subdirectories of an excluded Folders. In this case, my script will get all the ID of the folders below myFolder
and myPrivateFolder
. Here is my function :
$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'"))
As a result I have an array containing the ID of my excluded folder and all of its subdirectories. I then build the query, for each excluded folder ID, I add ...and 'newsubdirectoryID' not in parents. When running a request on a drive containing hundreds of subfolders, I have a 413 error saying that my client request is too large. How can I then exclude a folder and all of its content, without limit ?
Thank you in advance for any help
EDIT :
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);
}
}
"When running a request on a drive containing hundreds of subfolders, I have a 413 error saying that my client request is too large"
You will need to redesign your app.
Some approaches you can take:-
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.