Google Apps脚本:从云端硬盘下载文件(同一用户) [英] Google Apps Script: Downloading files from Drive (same user)

查看:92
本文介绍了Google Apps脚本:从云端硬盘下载文件(同一用户)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写Google Apps脚本来下载特定Drive文件夹中的所有文件(可能是.csv文件).我已经找到了getDownloadUrl()方法,但是我无法弄清楚该怎么做.我目前正在尝试以下代码,其中files是文件夹中文件的列表:

I'm trying to write a Google Apps Script to download all files in a particular Drive folder (likely .csv files). I have found the getDownloadUrl() method but I haven't been able to figure out what to do with it. I'm currently trying the following code, where files is the list of the files in the folder:

while(files.hasNext()) {
  var response = UrlFetchApp.fetch(files.next().getDownloadUrl());
  Logger.log(response.getContentText());
}

但是,当我尝试运行代码时,出现401错误,我猜这意味着我缺乏适当的授权?但是我给人的印象是,如果一切都在我的一个Google帐户中进行,则无需执行所有OAuth2步骤. 有关连接到外部API的Google指南使我看起来应该能够获取网址.我已经可以访问我的云端硬盘文件,因为运行该方法时下载URL确实存在.我在这里想念什么?我对这一切真的很陌生,所以也许这是基本的东西.

When I try to run the code, however, I get a 401 error which I guess means I lack the proper authorization? But I was under the impression that I wouldn't need to go through all of the OAuth2 steps if everything was taking place within my one Google account. The Google guide to connecting to external APIs makes it look like I should be able to just fetch the url. I've already gotten access to my Drive files, because the download URL does exist when I run that method. What am I missing here? I'm really new to all of this so maybe it's something basic.

谢谢!

我设法通过修改代码来解决401错误:

I managed to fix the 401 error by modifying the code as follows:

while(files.hasNext()) {
  var response = UrlFetchApp.fetch(files.next().getDownloadUrl(),{headers: {Authorization: "Bearer " + ScriptApp.getOAuthToken()}});
  Logger.log(response.getContentText());
}

但是问题仍然在于,这只会将内容返回给我,而不是下载文件.如何从此fetch通话结果中启动下载?

But the issue remains that this only returns the contents to me, rather than downloading the file. How can I initiate a download from the results of this fetch call?

推荐答案

除了列出所有下载链接之外,我想原始的发帖人也希望将文件下载到用户的计算机上(根据早期的

Besides listing all download links, I guess original poster also wants to download files to user's computer (according to earlier discussion).

要执行此操作,请在服务器端使用base 64编码blob(例如Google App Script),然后在客户端的浏览器中使用数据URI下载.下面是在此答案的帮助下实现的代码.

To do this, encode blob with base 64 in server side (e.g. Google App Script) and download with data URI in client's browser. Below are code for this, with help of this answer.

Google App脚本

Google App Script

...

function getBlobInBase64(fileId){
  // omit authorization code if any

  var file = DriveApp.getFileById(fileId);
  var blob = file .getBlob();
  
  return {
    file_name: file.getName(),
    mime: file.getMimeType(),
    b64: Utilities.base64Encode(blob.getBytes());
}

...

index.html

...

function getFile(fileId){
  google.script.run.withSuccessHandler((data) => {

    var uri = 'data:' + data.mime + ';charset=ISO-8859-1;base64,' + encodeURIComponent(data.b64);
    downloadURI(uri, data.file_name);

  }).withFailureHandler((err) => {
    console.log(err);
  }).getBlobInBase64();
}

...

注意:我没有运行此代码,但是该方法应该可以在其他项目中使用.

NOTE: I haven't run this code but the method should work as used in my other project.

这篇关于Google Apps脚本:从云端硬盘下载文件(同一用户)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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