google drive API search all children file with given folder ID - google-api

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.
}

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 list Direct children.
For each element in Direct children, filter All folders according to whether they have the corresponding direct child in parents. This way, you will retrieve a list of Grandchildren. Repeat the same process for each Grandchildren element, then for each Great-grandchildren and so on, until no folders are left. With this, you will have retrieved all the folders which are descendant of the main folder ID.
Once you have all the descendant folders, you have to make another Files: 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?

Related

Why all folders not included in IPMRootFolder using Redemption?

I know this wasn't an issue before but suddenly when i look at the folders that are under the RDOPstStore or RDOStore IPMRootFolder now i only get the standard folder names of Inbox, Deleted Items, Outbox, and Sent.
There is an additional custom named folder called "whalley-g" that i need to recursively iterate through.
This was working fine at one point for a year, but now i only get the four folders.
Redemption.RDOSession mySession = new Redemption.RDOSession();
Redemption.RDOPstStore pst = mySession.LogonPstStore(pstToProcess.PSTFullPathAndName, 1, "Outlook", 1, 1);
int numFolders = pst.IPMRootFolder.Folders.Count;
numFolders should be 5 but is only 4. I didn't bother with the recursive code since i just need to get by the fact that it isn't including the custom folder at all.

How to Add folder in Minio bucket using c#?

I am using Minio .net client library
my requirement is how can i store my files in folder structure like
ABC is Bucket, CMS is inner folder and CMS folder contains files so how can i achive this ?
amazon s3 doing same things using key value pair i.e ABC/CMS
EDIT:
How can i access files url in my .net project ?
Ex In ABC bucket i have abc.png file so how can i access image to display on HTML tag.
<img src="---any path---/abc.png">
Here is the solution of my own question.
var fileAsStreamData = file.OpenReadStream();
var fileName = "cms/" + file.FileName;
await _minioClient.PutObjectAsync("ASAP", objectName: fileName, data: fileAsStreamData, size: file.Length, contentType: file.ContentType);
Note:where "/" indicating folder structure in minio.
Ex. A/B/C/D/any_file_name so its consider as a inner folder like B is a inner folder of A and C is a inner folder of B and so on.
Creating an object whose name ends with a "/" will create a folder. It is an empty object that simulates a directory. link
I'd like to add that deleting the last object in a directory will also delete your folder. If you do not take that into account in your code, it might lead to bugs and malfunctioning.

Google Drive API v3, is there a way to get a list of folders that are parents of a fileId?

In v2 it was possible to make a call to /files with the query fileId in children to get a list of DriveFile objects that were parents of the supplied file.
Now, it seems to be required to make a call to /files/:fileId?fields=parents, then make a separate call to /files/:parentId for each returned parent, possibly turning one call into a dozen.
Is this correct, and if so why? This is a huge performance hit to our app, so hopefully there's an undocumented method.
The query "'fileId' in children'" doesn't publicly exist (not documented/supported) in v2 either and I don't recall it ever existing. What does exist in V2 is the Parents collection which effectively answers the same question. In v3, to get the parents of a file you just get the child and ask for the parents field.
As for whether or not that is a performance hit, I don't think it is in practice. The Parents resource in v2 was very light to begin with, and other than the ID the only useful field was the 'isRoot' property. That you can calculate yourself by calling files/root up front to get the ID of the root folder for that user (just once and save it, it won't change for that user.)
If you need to get more information about the parents than just the IDs and are worried about the # of calls you have to make, use batching to fetch them. If you just have one parent, no need to batch (it's just overhead.) If you find that a file has multiple parents, create a batch request. That'll be sent as a single HTTP request/response and is handled very efficiently on the back end.
Point is, if you just need IDs, it's no worse than before. It's one call to get the parents of a file.
If you need more than IDs, it's at most 2 HTTP requests (outside really bizarre edge cases like 1000+ parents which would exceed the batch size :)
In V3 it is possible to list all children of a parent as it's explained here: https://developers.google.com/drive/v3/web/search-parameters
Example call:
https://www.googleapis.com/drive/v3/files?q=parents in '0Byho0qAdzabmVl8xcDR1S0pNY3c' of course replace spaces with %20, this will list all the files in the folder which has id='0Byho0qAdzabmVl8xcDR1S0pNY3c'
you just need to mention like below:
var request = service.Files.List();
request.Q = "('root' in parents)";
var FileListOfParentOnly = request.Execute();

Is there a way to query for specific folder in Google drive API?

I want to retrieve all the files from a specific folder. Below code gives me all the files in my drive account.
$files = $service->files->listFiles($parameters);
Is there a way to query only a specific folder name?
I see that you are implementing with PHP. You can refer to the 'Children' resource, using the list method to show all the children within the folder you are looking for. Keep in mind that you will need the folder's id in order to list its content.
The code should be something like:
$children = $service->children->listChildren($folderId, $parameters);
You can find more information about this on: https://developers.google.com/drive/v2/reference/children/list

Firefox Add-on: Get the item id of an existing bookmark folder by foldername

I'm trying to make my add-on save bookmarks in it's own bookmark folder.
This function created the desired folder, but I want it to return the id of the existing folder instead of forever making millions of new ones if it can find it by name:
function special_bookmark_folder()
{
menuFolder = bmsvc_service().bookmarksMenuFolder;
// <Try and find the bookmark folder here!>
return bmsvc_service().createFolder(menuFolder, ":: Bookmarkr ::", bmsvc_service().DEFAULT_INDEX);
// else return id of existing folder if it's found here
}
You have to use the Places query APIs, which isn't the most straightforward task. Thankfully there is an example with exactly what you want

Resources