【发布时间】:2022-02-19 00:59:43
【问题描述】:
我正在使用Google Drive REST API 上传 ZIP 文件,但上传后我的所有 ZIP 文件都损坏了。当我下载文件然后尝试在我的计算机上解压缩它时,在 MacOS 上它显示“无法将 'FILE_NAME.zip' 扩展为 FOLDER。错误 79 - 文件类型或格式不正确。”。我通过让另一台计算机上的另一个人尝试解压缩它来确保它不仅仅是我的计算机,他们也遇到了同样的问题。我还确认 ZIP 文件在我将其上传到 Google 云端硬盘之前没有损坏。
以下是我的代码的简化版本。
const async = require('async');
const requestModule = require('request');
const fs = require('fs');
var api = {};
var tasks = {
// first, get the zip file contents
'getFile': function(cb) {
fs.readFile('my_file.zip', {'encoding':'UTF-8'}, function(err, data) {
if (err) {
console.error(err);
return cb();
}
api.file_data = data;
cb();
});
},
// second, upload the file contents to google drive via their API
'uploadFile': function(cb) {
var metadata = {
'mimeType': 'application/zip',
'name': 'my_file.zip'
};
var request = {
'url': 'https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart&supportsAllDrives=true',
'method': 'POST',
'headers': {
'Authorization': 'Bearer ' + GOOGLE_ACCESS_TOKEN,
'Content-Type': 'multipart/related; boundary="SECTION"'
},
'body': '--SECTION\r\n' +
'Content-Type: application/json; charset=UTF-8\r\n' +
'\r\n' +
JSON.stringify(metadata) + '\r\n' +
'\r\n' +
'--SECTION\r\n' +
'Content-Type: application/zip\r\n' +
'Content-Transfer-Encoding: base64\r\n' +
'\r\n' +
new Buffer.from(api.file_data).toString('base64') + '\r\n' +
'\r\n' +
'--SECTION--'
};
requestModule(request, function(err, res, body) {
if (err) {
console.error(err);
return cb();
}
cb();
});
}
};
async.series(tasks, function() {
console.log('Done');
});
注意:我正在做一个问答式的帖子,并将回答我自己的问题。
【问题讨论】:
标签: node.js google-drive-api async.js