Search code examples
google-apps-scriptfile-uploadgo-cd

Upload files from google drive to external api using google apps script


I am using google form to upload files to google drive. Now I want to be able to upload these (uploaded)files from google drive to external api using google apps script. (This api of GoCD to be specific.)

I tried to upload the file this way which is probably wrong, but it doesn't work:

function main() {
  var url = "https://<my-server-url>/go/files/Pipeline1/1/Stage1/1/Job1/folder/";
  var form = {
    file : DriveApp.getFileById("<file-id>"),
  };
  uploadFile(url,form);
}

function uploadFile(url,form) {
  
  var options = {
    "method" : "POST",
    "header" : {
    "Confirm" : true
    },
    "muteHttpExceptions" : true,
    'zipFile' : form
  };
  
  var response = UrlFetchApp.fetch(url,options);
  Logger.log("Response body: " + response.getContentText());
}

Is there any other way to make POST api call to upload files from drive? Also, any help with resources for doPost() method of google apps script will be appreciated.


Solution

  • Answering this question, if anyone needs it.

    So, I tried all the ways to upload files, but it won't work. Turns out you cannot directly get a file from drive and upload it to external api. You have to have a middleware (here, node app) where the file will be stored first locally and then upload it to api. I have used node app and Drive API V3 to download the file and then made a post call to external api.

    Thanks!