使用Google Drive API上传大文件 [英] Using Google Drive API to upload large files

查看:144
本文介绍了使用Google Drive API上传大文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图按照这篇文章中的答案(如何使用高级驱动器服务来上传文件)将我的上传脚本从使用DriveApps转换为Drive API以允许大文件上传(我需要能够上传大约50 GB的文件),但我没有没有太大的成功。

当我上传相对较小的文件时,一切正常(我尝试过20 MB左右的文件),但是当我尝试上传时一个较大的文件(大约400 MB),没有任何反应。



我得到以下错误:

POST 2601514732-mae_html_user_bin_i18n_mae_html_user.js:71 POST ... 3A1462412854269& fsid = 4787eea0-1d3c-4fd8-b263-8bae40da182d& func = uploadFiles 413()



375182757-mae_html_driver_bin_i18n_mae_html_driver.js:113 GET ... b263-8bae40da182d& token = AJuLMu2o9KnAOrSvzonQHNRGUelVpsakEg%3A1462412854269 50 0()



2601514732-mae_html_user_bin_i18n_mae_html_user.js:46 Uncaught NetworkError:HTTP 500导致连接失败



我以为Drive API应该允许我上传任何大小的文件?我究竟做错了什么?我检查并启用了Drive API。

我的server.gs脚本:

  function doGet(e){
return template = HtmlService.createHtmlOutputFromFile('form.html');
返回template.evaluate()。setSandboxMode(HtmlService.SandboxMode.IFRAME);
}

函数uploadFiles(表单){

尝试{

var dropbox =文件传输;
var文件夹,文件夹= DriveApp.getFoldersByName(dropbox);

if(folders.hasNext()){
folder = folders.next();
} else {
folder = DriveApp.createFolder(dropbox);
}


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

var resource = {
描述:上传的文件:+ 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); //将文件放入选定的文件夹

返回谢谢您的提交。

} catch(error){
return error.toString();
}
}

我的form.html:

 <!DOCTYPE html> 

<! - 您也可以包含您自己的CSS样式 - >
< link href ='https://fonts.googleapis.com/css?family = Bitter'rel ='stylesheet'type ='text / css'>

< div class =form-style-10>
< title>档案转移< / title>

< form id =myFormname =myForm>
< h1>文件传输< / h1>
< p>< / p>

< fieldset class =fields>
< div class =section>档案< / div>
< div class =inner-wrap>
< label for =File1>档案1< / label>
< input type =filename =File1required />

< / div>
< / fieldset>

< p> < / p为H.
< p id =incompleteWarningclass =hideClass>请选择要传输的文件。 < / p为H.
< p id =bePatientclass =hideClass>上传文件时请耐心等待。不要关闭或刷新表单。上传完成后,您会看到转档完成信息。< / p>

< input id =submitbuttontype =buttonvalue =Submit Application/>
< / form>

< div id =outputclass =hideClass>
< h1 id =TitleForm>文件传输< / h1>
< span id =ThankYou>转帐完成!如果您需要转移另一个文件,则可以再次使用相同的链接。
< / span>
< / div>

< / div>


< script type =text / javascript>

document.getElementById('submitbutton')。addEventListener(click,validatefunction);

函数validatefunction(){
document.getElementById('submitbutton')。val ='正在提交...';
//检查必填字段
var j = 0;
var form = document.getElementById('myForm');
var elem = form.elements;
for(var i = 0; i< elem.length; i ++){
elem [i] .className =;
if(elem [i] .value ===&& elem [i] .hasAttribute('required')){
elem [i] .className =warning;
j ++;



if(j === 0){
var btn = document.getElementById('submitbutton');
btn.disabled = true;
document.getElementById('incompleteWarning')。style.display ='none';
document.getElementById('bePatient')。style.display ='inline';
google.script.run.withSuccessHandler(fileUploaded).uploadFiles(this.parentNode);
} else {
document.getElementById('submitbutton')。val ='提交申请';
document.getElementById('incompleteWarning')。style.display ='inline';
document.getElementById('incompleteWarning')。style.color ='red';
}
};

< / script>

< script>
函数fileUploaded(status){
document.getElementById('myForm')。style.display ='none';
document.getElementById('output')。style.display ='inline';
}
< / script>

< style>
input {display:block; margin:20px; }
< / style>


解决方案

Apps脚本服务强制每日配额和硬性限制功能。



如果您超出配额或限制,您的脚本将抛出异常并终止执行。





在上传大文件时,脚本超时并因此显示错误。



同样,如果你有更高的上传速度,那么上传400MB也可以使用脚本。


I was trying to follow the answer in this post (How to Use Advanced Drive Service to Upload Files) to convert my upload script from using DriveApps to Drive API in order to allow large file uploads (I'll need to be able to upload files around 50 GB) but I haven't had much success.

Everything works fine when I'm uploading relatively small files (I've tried with files around 20 MB), but when I try and upload a larger file (around 400 MB), nothing happens.

I get the following errors:

POST 2601514732-mae_html_user_bin_i18n_mae_html_user.js:71 POST …3A1462412854269&fsid=4787eea0-1d3c-4fd8-b263-8bae40da182d&func=uploadFiles 413 ()

375182757-mae_html_driver_bin_i18n_mae_html_driver.js:113 GET …b263-8bae40da182d&token=AJuLMu2o9KnAOrSvzonQHNRGUelVpsakEg%3A1462412854269 500 ()

2601514732-mae_html_user_bin_i18n_mae_html_user.js:46 Uncaught NetworkError: Connection failure due to HTTP 500

I thought the Drive API was supposed to allow me to upload any size file? What am I doing wrong? I checked and I enable the Drive API. Any help is greatly appreciated.

My server.gs script:

 function doGet(e) {
   return template = HtmlService.createHtmlOutputFromFile('form.html');
   return template.evaluate().setSandboxMode(HtmlService.SandboxMode.IFRAME);
 }

 function uploadFiles(form) {

   try {

     var dropbox = "File Transfer";
     var folder, folders = DriveApp.getFoldersByName(dropbox);

     if (folders.hasNext()) {
       folder = folders.next();
     } else {
       folder = DriveApp.createFolder(dropbox);
     }


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

     var resource = {
       description: "File uploaded 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

     return "Thank you for your submission."

   } catch (error) {
     return error.toString();
   }
 }

My form.html:

<!DOCTYPE html>

<!-- You can also include your own CSS styles -->
<link href='https://fonts.googleapis.com/css?family=Bitter' rel='stylesheet' type='text/css'>

<div class="form-style-10">
<title>File Transfer </title>

<form id="myForm" name="myForm">
  <h1>  File Transfer </h1>
<p></p>

  <fieldset class="fields">
  <div class="section"> Files </div>
  <div class="inner-wrap">
    <label for="File1"> File 1 </label>
    <input type="file" name="File1" required />

  </div>
  </fieldset>

  <p>  </p>
  <p id="incompleteWarning" class="hideClass"> Please select a file to transfer. </p>
  <p id="bePatient" class="hideClass"> Please be patient while the file is being uploaded. Do not close or refresh the form. You will see a "transfer complete" message when the upload is finished.</p>

  <input id="submitbutton" type="button" value="Submit Application" />       
</form>

<div id="output" class="hideClass">
  <h1 id="TitleForm"> File Transfer </h1>
    <span id="ThankYou" >Transfer complete! If you need to transfer another file, you can use the same link again.
        </span>
</div>

</div>


<script type="text/javascript">

document.getElementById('submitbutton').addEventListener("click", validatefunction);

function validatefunction() {
   document.getElementById('submitbutton').val = 'Submitting...';
   //check for required fields
   var j = 0;
   var form = document.getElementById('myForm');
   var elem = form.elements;
   for (var i = 0; i < elem.length; i++){
     elem[i].className = "";
     if (elem[i].value === "" && elem[i].hasAttribute('required')){
       elem[i].className = "warning";
       j++;
    }
   }

   if (j === 0) {
       var btn = document.getElementById('submitbutton');
       btn.disabled = true;
       document.getElementById('incompleteWarning').style.display = 'none';
       document.getElementById('bePatient').style.display = 'inline';
           google.script.run.withSuccessHandler(fileUploaded).uploadFiles(this.parentNode);
    } else{
      document.getElementById('submitbutton').val = 'Submit Application';
      document.getElementById('incompleteWarning').style.display = 'inline';
      document.getElementById('incompleteWarning').style.color = 'red';
    }
};

</script>

<script>
    function fileUploaded(status) {
        document.getElementById('myForm').style.display = 'none';
        document.getElementById('output').style.display = 'inline';     
    }
</script>

<style>
 input { display:block; margin: 20px; }
</style>

解决方案

Apps Script services impose daily quotas and hard limitations on some features.

If you exceed a quota or limitation, your script will throw an exception and terminate execution.

While uploading a large file, the script time-outs and thus will show errors.

Again, if you have a higher upload speed then uploading 400MB might as well be possible using script.

这篇关于使用Google Drive API上传大文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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