Reputation: 1879
DISCUSSION
Google Drive API v3: Create Folder
CLOUD DRIVE APIs COMPARISON
Other cloud drive APIs when creating a folder, including Box API and Dropbox API, both return a conflict error if a folder of the same name already exists in parent folder.
However, Google Drive API by default allows creating folders of the same name.
QUESTION
Is there a way to return conflict error if trying to create a folder of the same name within Google Drive parent folder?
If I take the approach using API to search for folder by name first, then create folder if not exists, then this could result in a possible race condition by a parallel process doing the same task in creating a unique folder by name.
EXAMPLE
Google Drive API create folder call in curl
curl "https://www.googleapis.com/drive/v3/files" \
--request POST \
--verbose \
--write-out 'HTTPSTATUS:%{http_code}' \
--silent \
--header "authorization: Bearer [** ACCESS TOKEN **]" \
--header "cache-control: no-cache" \
--header "content-type: application/json; charset=utf-8" \
--header "Accept: application/json" \
--data '{ \
"mimeType":"application\/vnd.google-apps.folder", \
"name": "[** FOLDER NAME **]", \
"parents": ["root"] \
}'
Each call creates a new folder in Google Drive parent folder with unique folder id, however, same folder name. I wish to avoid this:
Success [HTTP status: 200]
{
"kind": "drive#file",
"id": "1mpy2-TVeZDTL8vZ6fKHTyoGoFHX-18EN",
"name": "TEST",
"mimeType": "application/vnd.google-apps.folder"
}
...
Success [HTTP status: 200]
{
"kind": "drive#file",
"id": "1iqYnEWOVFcWO3jWX1IgIv2wxtGVYruQX",
"name": "TEST",
"mimeType": "application/vnd.google-apps.folder"
}
I appreciate any assistance in getting a single call approach to either return conflict error or auto-renaming.
Upvotes: 3
Views: 1050
Reputation: 201378
If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.
At Google Drive, all files and folders are managed by the unique ID. By this, the files and folders with the same name can be created in a folder. When Drive API is used, this uses the ID which is not the filename and folder name. So in order to achieve your goal with Drive API, it is required to search the folder name. And also, the exclusive processing is required to for prevent to be created the same folder name with the asynchronous process. By this, unfortunately, your goal cannot be achieved with only the simple curl command.
From above situation, in order to achieve your goal with only the simple curl command, I would like to use Web Apps as an API as a workaround. Web Apps is created by Google Apps Script and this can be used like an API. By this, I thought that your goal can be achieved by this workaround.
In order to use this workaround, please do the following flow.
Create new Google Apps Script project.
Copy and paste the following sample script. This is used as the script of server side.
function doGet(e) {
var lock = LockService.getScriptLock();
if (lock.tryLock(10000)) {
var parent, folder;
if (e.parameter.parentId) {
parent = DriveApp.getFolderById(e.parameter.parentId);
folder = parent.getFoldersByName(e.parameter.folderName);
} else {
parent = DriveApp.getRootFolder();
folder = DriveApp.getFoldersByName(e.parameter.folderName);
}
if (folder.hasNext()) {
lock.releaseLock();
var err = {error: "Same folder name is existing."};
return ContentService.createTextOutput(JSON.stringify(err)).setMimeType(ContentService.MimeType.JSON);
}
var folderId = parent.createFolder(e.parameter.folderName).getId();
lock.releaseLock();
var msg = {message: "Done.", folderId: folderId};
return ContentService.createTextOutput(JSON.stringify(msg)).setMimeType(ContentService.MimeType.JSON);
}
}
Deploy Web Apps.
https://script.google.com/macros/s/###/exec
.
Use the deoloyed Web Apps with the following curl command.
curl -L \
-H "Authorization: Bearer ###" \
"https://script.google.com/macros/s/###/exec?parentId=###&folderName=###"
parentId
and folderName
are used as the query parameter.folderName
is existing in the folder of parentId
, {"error":"Same folder name is existing."}
is returned.folderName
is NOT existing in the folder of parentId
, new folder is created and {"message":"Done.","folderId":"### folder ID of created folder ###"}
is returned.If I misunderstood your question and this was not the direction you want, I apologize.
Upvotes: 2