问题
Here is how my folder is structured in google drive:
Picture
|----------Date1
|-----Pic1.png
|-----Pic2.png
|----------Date2
|-----Pic3.png
|-----Pic4.png
Right now I only have the ID of Picture folder (the parentID folder). Now I want to get Pic1 picture (inside Date1 folder). Is it possible to query only 1 time to get the picture with only the Picture folder's ID or I will have to query multiple times (get the Date1 and Date2 folder ID, then continue querying) ?
Here is my code right now
$imageName= "Pic1" // name of the image I want to find, which is Pic1
$folderId = "PictureFolderId" // Folder Picture's ID
$optParams = array(
'pageSize' => 1,
'fields' => 'nextPageToken, files(contentHints/thumbnail,fileExtension,id,name,size)',
'q' =>"mimeType contains 'image/' AND name contains '".$imageName."' AND '".$folderId."' in parents"
);
$results = $googleDriveService->files->listFiles($optParams);
if (count($results->getFiles()) == 0) {
print "No files found.\n";
} else {
// Do something.
}
回答1:
If I understand you correctly, you want to:
- Retrieve all the image files which are descendants (children, grandchildren, etc.) of a certain folder.
- You want to retrieve this with a single API call.
Answer:
It is currently not possible to retrieve files in sub-folders with a single call. That's because a File resource (and a "folder" is a File
) does not have information on what files are contained in this parent folder (let alone files contained in "sub-folders") and it only has information on the immediate parents of this folder, and not grandparents and so on, so to speak.
In order to retrieve all "descendants" of a certain folder, two main methods can be used (largely based on this answer by pinoyyid):
Method 1. List all folders:
- List all folders in your Drive via Files: list, disregarding whether they have the main folder ID in parents. Let's call this list
All folders
. - Filter
All folders
, according to whether they have the main folder ID in parents. Let's call this listDirect children
. - For each element in
Direct children
, filterAll folders
according to whether they have the correspondingdirect child
in parents. This way, you will retrieve a list ofGrandchildren
. Repeat the same process for eachGrandchildren
element, then for eachGreat-grandchildren
and so on, until no folders are left. With this, you will have retrieved all the folders which aredescendant
of the main folder ID. - Once you have all the
descendant
folders, you have to make anotherFiles: list
to find the images which are children of any of those folders, or from the main folder. The search query could be something like this:
mimeType contains 'image/' AND ('mainFolderId' in parents OR 'descendant1ID' in parents OR 'descendant2ID' in parents OR...)
- Note: This method requires less API calls and will be more appropriate if the file tree is relatively large.
Method 2. Iterate recursively:
- Use Files: list to list all files in your main folder which are either folders (and so their own children should be listed) or images. In this case, the search query would be something like:
(mimeType = 'application/vnd.google-apps.folder' OR mimeType contains 'image/') AND 'folderId' in parents
- For each child returned by this call, check if it's an image (MIME type containing
image/
) or a folder (MIME type:application/vnd.google-apps.folder
). For each folder, list all files contained in it, using the method from previous step. - Repeat these two steps until there is no folder left. All the images you have retrieved are the "descendants" of your main folder.
Notes:
There is an open feature request in Issue Tracker to include ancestors
in query searches, which could allow the retrieval of children in subfolders, and making this whole process much more easy. You could click the star on the top left of the referenced page in order to keep track of this request and to help prioritize it:
- Issue #111763024. When will you allow us to use "ancestors" field in searches?
Reference:
- How do I search sub-folders and sub-sub-folders in Google Drive?
来源:https://stackoverflow.com/questions/62652183/google-drive-api-search-all-children-file-with-given-folder-id