如果您下载目录,Google云端硬盘Web界面可让您下载单个.zip文件。但是,我发现使用API无法做到这一点。是否可以使用API在驱动器上创建多文件zip?
更新:Tanakie的代码有效!这很棒!但是,我只能在我的个人帐户中使用它。我有两个G-Suite管理员帐户,我收到错误:
"抱歉,此时无法打开文件。
请检查地址,然后重试。"
我已经确认这可以在多个免费的个人Google帐户中使用。 知道为什么在使用付费谷歌帐户时它不起作用吗?
-------更新------
如果您收到"抱歉,此时无法打开文件"错误,等一天,该错误将自行解决。
这是我的最终解决方案,只有POST。就像Tanaike在下面说的那样。
Google Script:
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。我找到了一个不依赖谷歌的解决方案...希望在我开始之前就已经知道了。
答案 0 :(得分:4)
这个解决方法怎么样?
不幸的是,没有用于在Google API中直接从外部创建zip文件的API。但我认为有一个解决方法。 Google Apps脚本(GAS)中有zip()
类实用程序方法。还有Web Apps作为从外部使用GAS的服务。我认为你想要做的事情可以通过组合来实现。
zip()
方法创建用于创建zip文件的GAS脚本。如果这对你没用,我很抱歉。
以下示例脚本怎么样?这是一个简单的示例脚本,用于为文件夹中的文件创建zip文件。
这些与网络界面相同。
请执行以下操作。
function doGet(e) {
var files = DriveApp.getFolderById(e.parameter.folderid).getFiles();
var fileIds = [];
while (files.hasNext()) {
fileIds.push(files.next().getId());
}
return ContentService.createTextOutput(zipping(fileIds));
}
function zipping(fileIds) {
var zipfilename = "sample.zip";
var blobs = [];
var mimeInf = [];
var accesstoken = ScriptApp.getOAuthToken();
fileIds.forEach(function(e) {
try {
var file = DriveApp.getFileById(e);
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=" + e + "&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/" + e + "/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/" + e + "?alt=media", {
method: "GET",
headers: {"Authorization": "Bearer " + accesstoken},
muteHttpExceptions: true
}).getBlob().setName(name);
}
blobs.push(blob);
});
var zip = Utilities.zip(blobs, zipfilename);
return DriveApp.createFile(zip).getId();
}
curl -L "https://script.google.com/macros/s/#####/exec?folderid=### folder ID ###"
如果这对你有用,我很高兴。