How to Use Advanced Drive Service to Upload Files

2020-04-18 08:53发布

问题:

I have the following Google Apps Script that takes a file from an upload form and stores it automatically in my Google Drive (full code in snippet below). The problem is with this section of it:

    var file = folder.createFile(blob);

        //Get root folder and pull all existing folders, plus setup variables pulled from form   
        var dropbox = form.Country;
        var filename = form.reportType+".xls";
        var rootfolder = DriveApp.getFolderById("0Byvtwn42HsoxfnVoSjB2NWprYnRiQ2VWUDZEendNOWwwM1FOZk1EVnJOU3BxQXhwU0pDSE0");
        //Note root folder is Live Uploads Folder in Flatworld App folder structure
        var folder, folders = rootfolder.getFoldersByName(dropbox);
        
        //Check if folder exists and if not create    
        if (folders.hasNext()) {
          folder = folders.next();
        } else {
          folder = rootfolder.createFolder(dropbox);
        }
        
        //Check if file already exists and delete if it does
        var file, files = folder.getFilesByName(filename);
        while( files.hasNext()){
          file = files.next();
          file.setTrashed(true);
        }
        
        
        //Upload file and set various properties
        var blob = form.myFile;    
        var file = folder.createFile(blob);
        var timeStamp = new Date();
        file.setDescription("Uploaded via BNI Upload Form by " + form.myName + " on: " + timeStamp);
        
        //Set file name slightly differently for Weekly Member Report (do not want to overright based on name just keep each extract so add timestamp to name)
        if (form.reportType == "Member Weekly"){
        file.setName(form.reportType + timeStamp + ".xls");
        }
      else
      {
        file.setName(filename);
      }

I'm using the standard Google App Services createFile method of the DriveApp class but this has a small file upload size and is proving very slow to upload larger files close to this limit.

I would like to move to the Advanced Google Services that allow you to use Google's public API methods direct in Google Apps Scripts. I've enabled the Drive API and think I want to use something like this:

function uploadFile() {
  var image = UrlFetchApp.fetch('http://goo.gl/nd7zjB').getBlob();
  var file = {
    title: 'google_logo.png',
    mimeType: 'image/png'
  };
  file = Drive.Files.insert(file, image);
  Logger.log('ID: %s, File size (bytes): %s', file.id, file.fileSize);
}

But I'm struggling to implement it alongside my existing code, and the example languages are all for those coding in other languages and using the API rather than just writing in Apps Script.

Please can anyone suggest the code revision required to use the advanced method, allowing a quicker and larger upload?

回答1:

There is a bit of to'ing and fro'ing possible between Drive and DriveApp rather than getting knee-dep into the pure Drive API.

//Upload file and set various properties
var mediaData = form.myFile;    
var timeStamp = new Date();

var resource = {
  title: (form.reportType == "Member Weekly") ? form.reportType + timeStamp + ".xls" : filename,
  mimetype: 'application/vnd.ms-excel',
  description: "Uploaded via BNI Upload Form by " + form.myName + " on: " + timeStamp
};

var file = Drive.Files.insert(resource, mediaData); // create file using Drive API
var fileId = file.id;    
var DriveAppFile = DriveApp.getFileById(fileId); // retrieve file in DriveApp scope. 

DriveApp.removeFile(DriveAppFile); // remove new file from Users root My Drive
folder.addFile(DriveAppFile); // puts file in selected folder

Drive in this code refers to the Drive API Advanced Google Service that is enabled from the Resources menu. You'll need to enable it as an API from the developer console also. These advanced services attach themselves to your script much like Liraries. Drive here is what the library is defaulting to.

How to enable Advanced Services