文件列表,不包括文件夹及其所有的子子目录 [英] Files list with an exclusion of folders and all its grand children subdirectories

查看:587
本文介绍了文件列表,不包括文件夹及其所有的子子目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的驱动器中有这样的层次结构:

   -  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){
printUne erreur s'est produite:。 $ E->的getMessage();
}
返回$ result;

作为参数,我在提取排除的文件夹ID后给它们:

  $ parameters = array('q'=>'父母不是'myFolderID',父母不是'myprivateFolderID'); 

运行我的脚本时,我只希望得到 logo1.png logo2.png ,因为我排除了 myFolder myPrivateFolder ,但我还得到 logo4.png ,因为它有 anotherPrivateFolder myPrivateFolder 是祖父母。我了解Google Drive API没有树结构的概念,因此我制作了一个递归函数来传播排除文件夹的所有子目录。在这种情况下,我的脚本将获得 myFolder myPrivateFolder 下所有文件夹的ID。这是我的功能:

  $ excluded_folders_id = array(); 
函数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){
printUne erreur s'est produite:。 $ E->的getMessage();
$ pageToken = NULL;
}
} while($ pageToken);
foreach($ results as $ result){
echo=>要排除的新子项:。$ result->名称。\\\
;
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错误,说我的客户端请求太大。我怎样才能排除一个文件夹及其所有内容,没有限制?



预先感谢您的帮助



编辑:

 函数isFileInExcluded($ service,$ entity_id){

global $ excluded_folders_id ;

$ entityProperties = $ service-> files-> get($ entity_id,array('fields'=>'parents,id,name'));

如果没有更多的父母(所以如果在树顶部 - 没有遇到排除)
if($ entityProperties-> parents == NULL){
return true;
}
//如果文件排除了树上部的文件夹
if(in_array($ entityProperties-> parents [0],$ excluded_folders_id)== true){
返回true;
} else {
//展开acrosse上级父项
$ parentProperties = $ service-> files-> get($ entityProperties-> parents [0],array('fields' ='父母,身份证,姓名'));
isFileInExcluded($ service,$ parentProperties-> id);
}
}


解决方案

当在包含数百个子文件夹的驱动器上运行请求时,我有413错误,说我的客户端请求太大 您会需要重新设计你的应用程序。



您可以采用以下方法: - $ / $>


  1. 找不到您要检索的文件通用的包含参数。

  2. 只需获取所有图像,然后在php中过滤出所需的图像。

  3. 提供您希望排除共同父母的图像。所以例如。你的logo4.png是另一个私人文件夹的孩子。创建一个名为sayexcludeAllChildren的新文件夹,并使除另一个私人文件夹的子项外的logo4.png成为excludeAllChildren的子项。


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:-

  1. Instead of excluding, can you not find an inclusive parameter that is common to the files you want to retrieve.
  2. Simply fetch all images and then filter out the required ones in your php.
  3. Give the images you wish to exclude a common parent. So eg. your logo4.png is a child of anotherPrivateFolder. Create a new folder called say "excludeAllChildren", and make logo4.png a child of excludeAllChildren in addition to a child of anotherPrivateFolder.

这篇关于文件列表,不包括文件夹及其所有的子子目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆