如果您下载目录,Google 云端硬盘网络界面允许您下载单个 .zip 文件。但是,我找不到使用 API 来做到这一点的方法。是否可以使用 API 在驱动器上创建多文件 zip?
更新:Tanakie 的代码有效!这很棒!但是,我只能在我的个人帐户中使用它。我有两个 G-Suite 管理员帐户,但收到错误:
“抱歉,目前无法打开该文件。
请检查地址并再试一次。”
我已经确认这在多个免费个人谷歌帐户中运行良好。
知道为什么它在使用付费谷歌帐户时不起作用吗?
- - - - 更新 - - -
如果您收到“抱歉,此时无法打开文件”错误,请稍等一天,该错误将会自行修复。
这是我仅使用 POST 的最终解决方案。和田池下面说的差不多。
谷歌脚本:
function doPost(e) {
var zipFileName = e.parameter.zipFileName
var fileIds = e.parameter.ids.split(",");
return ContentService.createTextOutput(zipping(fileIds, zipFileName));
}
function zipping(fileIds, zipfilename) {
var blobs = [];
var mimeInf = [];
var accesstoken = ScriptApp.getOAuthToken();
fileIds.forEach(function(fileID) {
try {
var file = DriveApp.getFileById(fileID);
var mime = file.getMimeType();
var name = file.getName();
} catch (er) {
return er
}
var blob;
if (mime == "application/vnd.google-apps.script") {
blob = UrlFetchApp.fetch("https://script.google.com/feeds/download/export?id=" + fileID + "&format=json", {
method: "GET",
headers: {"Authorization": "Bearer " + accesstoken},
muteHttpExceptions: true
}).getBlob().setName(name);
} else if (~mime.indexOf('google-apps')) {
mimeInf =
mime == "application/vnd.google-apps.spreadsheet" ? ["application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", name + ".xlsx"] : mime == "application/vnd.google-apps.document" ? ["application/vnd.openxmlformats-officedocument.wordprocessingml.document", name + ".docx"] : mime == "application/vnd.google-apps.presentation" ? ["application/vnd.openxmlformats-officedocument.presentationml.presentation", name + ".pptx"] : ["application/pdf", name + ".pdf"];
blob = UrlFetchApp.fetch("https://www.googleapis.com/drive/v3/files/" + fileID + "/export?mimeType=" + mimeInf[0], {
method: "GET",
headers: {"Authorization": "Bearer " + accesstoken},
muteHttpExceptions: true
}).getBlob().setName(mimeInf[1]);
} else {
blob = UrlFetchApp.fetch("https://www.googleapis.com/drive/v3/files/" + fileID + "?alt=media", {
method: "GET",
headers: {"Authorization": "Bearer " + accesstoken},
muteHttpExceptions: true
}).getBlob().setName(name);
}
blobs.push(blob);
});
var zip = Utilities.zip(blobs, zipfilename);
var driveFolder = DriveApp.getFoldersByName("zipFiles").next();
return driveFolder.createFile(zip).getId();
}
调用它的php代码:
$url = 'https://script.google.com/a/domain.com/macros/s/### script id ####/exec';
$googleIDs = array();
foreach($documents AS $document){
$googleIDs[] = $document->google_file_id;
}
$data['zipFileName'] = date('c') . ".zip";
$data['ids'] = join(',', $googleIDs);
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array( "Content-type: multipart/form-data" ));
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$result = curl_exec($ch); // This returns the Google file ID of the zip file.
curl_close($ch);
然而,正如我在下面的评论中所说,这是一个无用的、徒劳的练习,因为 Google 将脚本创建的文件限制为 10M。我要去寻找一个不依赖谷歌的解决方案......希望我在开始之前就知道这一点。