【问题标题】:Using Google Drive API to upload large files使用 Google Drive API 上传大文件
【发布时间】:2016-05-05 01:55:12
【问题描述】:

我试图按照这篇文章 (How to Use Advanced Drive Service to Upload Files) 中的答案将我的上传脚本从使用 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 500 ()

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

我认为 Drive API 应该允许我上传任何大小的文件?我究竟做错了什么?我检查并启用了 Drive API。非常感谢任何帮助。

我的 server.gs 脚本:

 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();
   }
 }

我的表单.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>

【问题讨论】:

  • @noogui 我假设我使用的是简单上传类型。我不确定如何在我的脚本中更改它。
  • 检查这个repo,它可能会给你一个洞察力。
  • 我不完全确定,但 uploadType 无关紧要,因为您使用的是 Advance Drive Service,而不是 Drive REST API。
  • 这已在此thread 中进一步讨论。阅读发布的答案,简单上传仅适用于 5MB 等较小的文件,当您开始遇到较大文件的问题时,这一点很明显。尝试使用 Drive.Files.insert(resource, mediaData,{uploadType:'resumable'}); 添加上传类型
  • 添加 {uploadType: 'resumable'} 无效。事实上,我认为这会导致脚本也无法处理小文件上传。

标签: javascript html google-apps-script google-drive-api


【解决方案1】:

Apps 脚本服务对某些功能施加每日配额和硬性限制。

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

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

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

【讨论】:

    猜你喜欢
    • 2018-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-07
    • 1970-01-01
    • 2017-02-17
    • 1970-01-01
    相关资源
    最近更新 更多