2
votes

I have a JavaScript based Google Apps script that iterates through all files in Drive and converts MS files to Google format. I am using Drive API to upload and convert. Everything seems to work fine, except when I have a file in a Drive sub folder. When the file is uploaded back to Drive, I cannot define a parent folder, so it goes directly to the root.

I have a handle on the parent folder ID, but when I add "parents : folderID" to the JSON String, I do not see any change in the upload path.

This is my upload and convert function, is there any way to modify this to define a parent folder? I'm totally lost with the API... Can anyone assist?

 var uploadFile = JSON.parse(
 UrlFetchApp.fetch("https://www.googleapis.com/upload/drive/v2/files?uploadType=media&convert=true", 
 {
   method: "POST",
   contentType: officeFile.getMimeType(),
   payload: officeFile.getBlob().getBytes(),
   parents: ['id' : folderID], <<<<<<<<<<<<<<<<< this line
   headers: {
     "Authorization" : "Bearer " + ScriptApp.getOAuthToken()
   },
   muteHttpExceptions: true
 }).getContentText());
2
Looking at the documentation for the parents[ ] description. I guess as long as you set the folderID right you should be able to upload to the folder. Please double check your folderID. Also, there is already a v3 available for the Drive API so I think that it is worth looking at. Good luck!gerardnimo
I definitely had the parent ID correct (logged both the root and sub folder ID). No matter how I formatted the JSON string the file always uploaded to the root. I had to use a ridiculous work around which uploaded to the root and then moved to the sub folder. Since you can't "move" files in Drive API I had to "copy" and then delete the original. What a crap work around, but it got the job done. I was finally able to finish the script. It's a complete MS to Google converter for Drive with permission mapping. Thanks for the comment gerardnimo. I will check out the V3 API for future updates!Dougie

2 Answers

8
votes

With V3 of the API the format of the parents parameter has changed (didn't seem to be listed in the documentation which always makes things fun). Parents are no longer added as {id:} objects.

var folderId = '0BwwA4oUTeiV1TGRPeTVjaWRDY1E';
var fileMetadata = {
  'name': 'photo.jpg',
  'parents': [ folderId ]
};
var media = {
  mimeType: 'image/jpeg',
  body: fs.createReadStream('files/photo.jpg')
};
drive.files.create({
   resource: fileMetadata,
   media: media,
   fields: 'id'
}, function(err, file) {
  if(err) {
    // Handle error
    console.log(err);
  } else {
    console.log('File Id: ', file.id);
  }
});

Full documentation

0
votes

I use v2 API with curl and succeeded to upload file in parent folder by writing meta json file with below format.

{
        "title": "35_20190702.csv",
        "mimeType": "text/csv",
        "description": "Uploaded From Curl",
        "parents": [
                {
                        "kind": "drive#parentReference",
                        "id": ${FOLDER_ID}
                }
        ]
}