How to create folders recursively in googledrive through api? - google-drive-api

For example, I'm going to upload a file scenery.jpg to /images/2020/03/18/ directory, if I upload to OneDrive, the upload url going to be like this:
https://graph.microsoft.com/v1.0/me/drive/root:/images/2020/03/18/scenery.jpg
I don't have to know if the images folder, the 2020, 03 and 18 folder exists or not, OneDrive will auto create it recursively, acting like Object-based Storage.
But now, I'm going to upload it to GoogleDrive and GoogleDrive seems can't create folders recursively(as far as I know, see Create and populate folders), now I have 2 questions:
1、Is that I have to check if these folders exists one by one to make sure if I have to create these folders?
/images/
/images/2020/
/images/2020/03/
/images/2020/03/18/
2、If none of above folder exists, is that I have to create these four folders one by one(means request api four times)?
Anyone who did this before? I hope what I assume is wrong, because it's too complicated to do it in this way.

Unfortunately you are correct
Google handles file and folder hierarchy in a different way and indeed the only way create / list nested files and folders is to iterate recursively.
Also, if you want to know if a file / folder already exists with the method Files:get, you need to know the file / folder ID, rather than just the name. If you do not know the ID, than you need to list all files on your drive / (unless you specify a certain folder as the parent folder, e.g. '1234567' in parents with the query parameter q).
The same applies for creation. If you create a folder which you want to be a subfolder of a different parent folder - you also need to create the parent folder.
However, it is not as complicated as you may think.
Here is one of many available samples of how to list the contents of all subfolders and subfolders of subfolders dynamically - in your case you would just need to add the condition to create a certain folder if it is not contained in the list.
You would need to take some time to study the working principle of Drive API, however once you get an understanding it is not complicated.

Related

Moving a file or folder with DriveApp

Really, there is no way to move a file or folder using the DriveApp class?
From what I could gather on the docs and on the StackOverflow answered questions all proposed implementations seem to copy the file to another location and then delete the original file. That would result in at least two problems:
File/folder having a different folderId on the destination;
File/folder being duplicated and stored indefinitely on Google Vault by retention policy.
I must be doing something wrong. Why is there not a method to simply move the file/folder to another destination (as provided by the Drive Web UI)?
Thanks,
When it comes to moving files with DriveApp, you need to work from the Folder rather than the File.
Open the folder you want to move the file to and use the "addFile(file)" method to add the file to that folder, then open the folder you want to move the file from and use "removeFile(file)" to remove it.
This might seem a bit clunky but it's actually possible to have a file in more than one folder at a time on Drive. The folders are really just labels. When considering a file that is already in multiple folders, the meaning of "moving" it to a new folder is kind of ambiguous, it's really a matter of adding/removing it from folders.
Add:
https://developers.google.com/apps-script/reference/drive/folder#addfilechild
Remove:
https://developers.google.com/apps-script/reference/drive/folder#removeFile(File)
Stumbled across this post (two years & 6 months later) and just wanted to point out that its now possible to do this:
DriveApp.getFileById(myFileId).moveTo(DriveApp.getFolderById(myTargetFolderId));

I want to find some files that includes some keyword and copy it to other folder

I want to find files that includes some keyword and copy it to other folder.
So I need two features.
Find files by keyword and list it at "file" type value.
copy the file to different directory.
But Google drive supports same folder name so I don't know how to targeting folder by name. And I want copy the file only if there is not file has same name. Because I'll run this script every 5 minutes and if I don't set about that, there are many same name files to target directory.
How can I do this?
AFAIK, the only supported methods that you can use to get files in Class DriveApp are:
getFileById(id)
getFiles()
getFilesByName(name)
getFilesByType(mimeType)
Then, to make a copy to other folder, you can use the following methods that are given in Class File
makeCopy(destination)
makeCopy(name, destination)
Lastly, the suggested solutions given in this SO post might also help.

Drive SDK: locating files in directories

I am using the Google Drive SDK to implement cloud storage for my iOS app. One thing I'm unclear on from the docs is how to query for files deep in the Drive directory structure.
I can make a query like:
title = 'TheFileTitle' and '1234567' in parents
But how to I find the fileID '1234567'? I am creating a directory structure to store my data in the cloud but I only know the subdirectory's name. How do I get the fileID that is needed for this query?
I could query for that directory and look for the fileID in the metadata but that just raises the question again. That directory name may not be unique so I would have to know its parent's fileID to locate the correct one.
Any help clarifying this would be appreciated.
Drive offers very little in the way of folder tree navigation. This is for the very good reason that folders are simply labels attached to files, rather than a strict hierarchy. So for example a file can have two or more parent folders. Although I've never tried it (I'm frightened it would create a black hole and universe would end) it's possible to create a loop where folder-A has child folder-B has child folder-C has child folder-A. Please don't try this at home, kids.
So the answer to your question, is start by querying for all folders and follow their parent arrays to build up an in-memory hierarchy. Then use that to navigate to a given file.
I would also suggest that you ask yourself the question whether deeply nested folders is a good idea. The answer may well be 'yes', but you should at least consider alternatives. Remember, folders were an afterthought in Drive, so might not be all they seem.

Is there any easy way to get folderID based on a given path?

Box api is implemented to be RESTful. and most supported methods are based on ids, folder_id or file_id.
As a very beginning start point, a root folder id, 0, stands for the root directory /All Files/.
from there (fold_id = 0), I can loop through all sub folders and find folder id for certain target folder.
or I can send search request to "https://api.box.com/2.0/search?query=target_folder_name", and process the response to locate target folder.
The former approach may need multiple list-folder-item requests, and the latter approach might be slow because the search is not just for folder/file name but also for other attributes of folder/file and even file content.
I am wondering if there is an easy way to find folder id for certain folder with a given path, such as "/All Files/MyFolder_A/Project_11".
Thank you very much for any help.
To the best of my knowledge, walking the folder tree (as you've suggested) is still the fastest way to get the ID of a particular file/folder. (See also this question: Get file ID of a given path.)

Is it possible to retrieve a file or folder resource by path?

Is there any way to get information about a file or folder on Box, given it's full path, but not ID?
For example, I'd like to check if /Foo/Bar/test.txt exists on Box. Currently, I have to start at the root and recursively walk through each folder level, searching for the next segment in the path.
As you can imagine, this process is very cumbersome when writing fully asynchronous code.
There currently is not a way to retrieve files or folders by path. This is something we may consider for the future, but don't have plans to implement it in the short term.