如何使用高级驱动器服务来上传文件 [英] How to Use Advanced Drive Service to Upload Files

查看:82
本文介绍了如何使用高级驱动器服务来上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我拥有以下Google Apps脚本,可从上传表单中提取文件并将其自动保存到我的Google云端硬盘中(完整代码位于下方代码段中)。问题出在这个部分:



var file = folder.createFile(blob);

//获取根文件夹并将所有现有文件夹,加上从表单拉出的设置变量var dropbox = form.Country; var filename = form.reportType +。xls; VAR rootfolder = DriveApp.getFolderById( 0Byvtwn42HsoxfnVoSjB2NWprYnRiQ2VWUDZEendNOWwwM1FOZk1EVnJOU3BxQXhwU0pDSE0); //注意根文件夹是Flatworld App文件夹结构中的活动上传文件夹var folder,folders = rootfolder.getFoldersByName(dropbox); //检查文件夹是否存在以及是否创建if(folders.hasNext()){folder = folders.next(); } else {folder = rootfolder.createFolder(dropbox); } //检查文件是否已经存在,并删除它是否存在var file,files = folder.getFilesByName(filename); while(files.hasNext()){file = files.next(); file.setTrashed(真); } //上传文件并设置各种属性var blob = form.myFile; var file = folder.createFile(blob); var timeStamp = new Date(); file.setDescription(通过+ form.myName +在+ timeStamp上通过BNI上传表单上传); //为每周成员报告设置文件名称略有不同(不要基于名称进行重写,只保留每个提取文件,以便将时间戳添加到名称中)if(form.reportType ==Member Weekly){file.setName(form.reportType + timeStamp +.xls); } else {file.setName(filename); }



我使用标准Google App Services <$ c DriveApp类的$ c> createFile 方法,但是这有一个小文件上传大小,并且证明上传较大文件接近此限制非常缓慢。



我想转到高级Google服务,以便您直接在Google Apps脚本中使用Google的公开API方法。我已经启用了Drive API,并认为我想要使用类似这样的内容:
$ b

  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,文件大小(字节):%s',file.id,file.fileSize);
}

但是我很努力的将它与我现有的代码一起实现,语言都适用于那些使用其他语言进行编码并使用API​​的人,而不仅仅是在Apps Script中编写。



请任何人都可以提出使用高级方法所需的代码版本,允许更快,更大的上传?

解决方案

有一点to'ing和fro'ing < Drive DriveApp ,而不是单纯的Drive API。

  //上传文件并设置各种属性
var mediaData = form.myFile;
var timeStamp = new Date();

var resource = {
title:(form.reportType ==会员周刊)? form.reportType + timeStamp +.xls:文件名,
mimetype:'application / vnd.ms-excel',
description:通过+ form.myName +上传BNI上传表单:+ timeStamp
};

var file = Drive.Files.insert(resource,mediaData); //使用Drive API创建文件
var fileId = file.id;
var DriveAppFile = DriveApp.getFileById(fileId); //在DriveApp范围内检索文件。

DriveApp.removeFile(DriveAppFile); //从用户root中删除新文件我的云端硬盘
folder.addFile(DriveAppFile); //将文件放入所选文件夹中

驱动器在此代码中指的是通过资源启用的 Drive API高级Google服务菜单。您还需要从开发者控制台启用它作为API。这些高级服务将自己附加到脚本中,就像Liraries一样。 Drive 这是图书馆默认的功能。 $ b

如何启用高级服务


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?

解决方案

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

这篇关于如何使用高级驱动器服务来上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆