How to upload an existing file in Drive to Drive using Apps Script

前端 未结 2 1686
粉色の甜心
粉色の甜心 2021-01-19 02:49

Introduction

Let me first introduce the goal of what I am trying to do.

  • I had a file split into two parts earlier

  • Si

2条回答
  •  说谎
    说谎 (楼主)
    2021-01-19 03:13

    Tanaike's answer is more than perfect. It's elegant and has even helped me to learn about array.reduce function. Before I asked this question, I had minimal knowledge about JavaScript and almost zero knowledge in using Google Drive API.

    My intention was to learn the whole process of resumable upload step by step using Google Apps Script as the language. Using Tanaike's code as reference I wrote a script which instead of being productive, manageable, and elegant would provide myself (at least) an idea of how resumable upload works step by step. I have used no loops, no objects, and even no arrays.

    Step 1 ( Declare the necessary variables )

      var fileId1 = "XXXXXXXXXXX"; //id of the first file
      var fileId2 = "YYYYYYYYYYY"; //id of the second file
      var filename = "merged.pdf"; //name of the final merged file
      var mimeType = MimeType.PDF; //Mime type of the merged file
    

    Step 2 ( Initiate the resumable upload )

    //declare the end point
    const url = "https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable";
    
    //Send the request
    //Method to be used is Post during initiation
    //No file is to be sent during initiation
    //The file name and the mime type are sent
    const res1 = UrlFetchApp.fetch(url, {
        method: "post",
        contentType: "application/json",
        payload: JSON.stringify({name: filename, mimeType: mimeType}),
        headers: {authorization: "Bearer " + ScriptApp.getOAuthToken()
      }});
    

    Step 3 ( Save the resumable session URI )

    const location = res1.getHeaders().Location;
    

    Step 4 (a) ( Upload file 1 )

    Note : Step 4 (a) and (b) can be performed using a loop. In my case, I used it two times without loop

      var file = DriveApp.getFileById(fileId1); //get the first file
      var data = file.getBlob().getBytes(); //get its contents in bytes array
    
    //Method used is PUT not POST
    //Content-Range will contain the range from starting byte to ending byte, then a slash
    //and then file size
    //bytes array of file's blob is put in data
      var params = {
        method : "put",
        headers : {
          'Content-Range' : `bytes 0-524287/687627`
        },
        payload : data,
        muteHttpExceptions: true
      }; 
    
    //Request using Resumable session URI, and above params as parameter
    
      var result = UrlFetchApp.fetch(location,params);
    

    Step 4 (b) ( Upload the second file )

    //Almost same as Step 4 (a)
    //The thing that changes is Content Range
    file = DriveApp.getFileById(fileId2);
      data = file.getBlob().getBytes();
    
      params = {
        method : "put",
        headers : {
          'Content-Range' : `bytes 524288-687626/687627`
        },
        payload : data,
        muteHttpExceptions : true
      };
    
      result = UrlFetchApp.fetch(location, params);
    

    Now instead of doing step 4 n number of times, it's better to use a loop.

    Also, this code doesn't checks for possible error that might have occurred during the process.

    Hope this code helps someone, even though it was more of a self-teaching experiment. :)

提交回复
热议问题