google-apps-scriptgoogle-drive-apibox-apiurlfetch

Uploading from Google Drive to BOX through AppScript


Currently working on a google sheet and implementing a script that will upload a file from Drive to BOX . I got the authentication down (I am able to get the folder list through the script connecting to the API) but when I get to the uploading part, I am getting an empty request and the file is not getting uploaded to the target folder

My script below:

function uploadFile() {

  var boundary = "test";
  var blob = DriveApp.getFileById('<I got my file ID here>').getBlob();
  var service = getService();

  var attributes = "{\"name\":\"test document.pdf\", \"parent\":{\"id\":\"<I got my folder ID here>\"}}";

  var requestBody = Utilities.newBlob(
    "--"+boundary+"\r\n"
    + "Content-Disposition: form-data; name=\"attributes\"\r\n\r\n"
    + attributes+"\r\n"+"--"+boundary+"\r\n"
    + "Content-Disposition: form-data; name=\"file\"; filename=\""+blob.getName()+"\"\r\n"
  + "Content-Type: " + blob.getContentType()+"\r\n\r\n").getBytes()
  .concat(blob.getBytes())
  .concat(Utilities.newBlob("\r\n--"+boundary+"--\r\n").getBytes());

  var options = {
    method: "post",
    contentType: "multipart/form-data; boundary="+boundary,
    payload: requestBody,
    muteHttpExceptions: true,
    headers: {'Authorization': 'Bearer ' + service.getAccessToken()}
  };

  var request = UrlFetchApp.fetch("https://upload.box.com/api/2.0/files/content", options);

  Logger.log(request.getContentText()); // empty response

}

Thanks


Solution

  • Modification points:

    When above curl command is converted to Google Apps Script, it becomes as follows.

    Modified script:

    function uploadFile() {
      var blob = DriveApp.getFileById('<I got my file ID here>').getBlob();
      var service = getService();
      var attributes = {"name":"test document.pdf", "parent":{"id":"<I got my folder ID here>"}};
      var requestBody = {attributes: Utilities.newBlob(JSON.stringify(attributes), "application/json"), file: blob};
      var options = {
        method: "post",
        payload: requestBody,
        muteHttpExceptions: true,
        headers: {'Authorization': 'Bearer ' + service.getAccessToken()}
      };
      var request = UrlFetchApp.fetch("https://upload.box.com/api/2.0/files/content", options);
      Logger.log(request.getContentText());
    }
    

    Note:

    References: