【问题标题】:Upload files from google drive to external api using google apps script使用谷歌应用脚​​本将文件从谷歌驱动器上传到外部 api
【发布时间】:2020-03-13 17:34:06
【问题描述】:

我正在使用谷歌表单将文件上传到谷歌驱动器。现在我希望能够使用谷歌应用脚​​本将这些(上传的)文件从谷歌驱动器上传到外部 api。 (具体是ThisGoCD的api。)

我尝试以这种方式上传文件,这可能是错误的,但它不起作用:

function main() {
  var url = "https://<my-server-url>/go/files/Pipeline1/1/Stage1/1/Job1/folder/";
  var form = {
    file : DriveApp.getFileById("<file-id>"),
  };
  uploadFile(url,form);
}

function uploadFile(url,form) {
  
  var options = {
    "method" : "POST",
    "header" : {
    "Confirm" : true
    },
    "muteHttpExceptions" : true,
    'zipFile' : form
  };
  
  var response = UrlFetchApp.fetch(url,options);
  Logger.log("Response body: " + response.getContentText());
}

还有其他方法可以通过 POST api 调用从驱动器上传文件吗? 此外,任何有关谷歌应用程序脚本的 doPost() 方法资源的帮助将不胜感激。

【问题讨论】:

    标签: google-apps-script file-upload go-cd


    【解决方案1】:

    查看我的帖子和@Tanaike 的回答,因为我认为我找到的答案也回答了您的问题,@Shivani-Shinde:How do you create and then insert an image file from Google Drive into a Google Apps Script UrlFetchApp.fetch call to an External API?

    var formData = {
      'upload': DriveApp.getFileById("###").getBlob()
    };
    

    【讨论】:

    • 我已经尝试过这段代码,它工作正常,但文件保存为 PDF。你知道如何将 blob 数据保存为 .docx 吗?
    【解决方案2】:

    回答这个问题,如果有人需要的话。

    所以,我尝试了所有上传文件的方法,但都不行。事实证明,您不能直接从驱动器获取文件并将其上传到外部 api。您必须有一个中间件(此处为节点应用程序),其中文件将首先存储在本地,然后将其上传到 api。我已经使用 node app 和 Drive API V3 下载了文件,然后对外部 api 进行了 post 调用。

    谢谢!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-16
      • 1970-01-01
      • 2013-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多