我正在使用nodejs模块googleapis 7.1.0从Google云端硬盘下载文件。当我进行身份验证或检索元数据时,一切都很顺利。
当下载完成并且应用程序应该结束时,我会得到两个不同的结果,两者似乎都是错误的。
在Windows中,程序只是无限期挂起并且不产生输出,也没有例外。我只是挂了。
在FreeBSD上,我得到以下堆栈跟踪:
buffer.js:377
throw new Error('toString failed');
^
Error: toString failed
at Buffer.toString (buffer.js:377:11)
at BufferList.toString (/usr/home/jvavruska/gdrive/node_modules/googleapis/node_modules/google-auth-library/node_modules/request/node_modules/bl/bl.js:166:33)
at Request.<anonymous> (/usr/home/jvavruska/gdrive/node_modules/googleapis/node_modules/google-auth-library/node_modules/request/request.js:1035:36)
at emitOne (events.js:82:20)
at Request.emit (events.js:169:7)
at IncomingMessage.<anonymous> (/usr/home/jvavruska/gdrive/node_modules/googleapis/node_modules/google-auth-library/node_modules/request/request.js:1003:12)
at emitNone (events.js:72:20)
at IncomingMessage.emit (events.js:166:7)
at endReadableNT (_stream_readable.js:913:12)
at nextTickCallbackWith2Args (node.js:442:9)
我在两台机器(Windows 10和FreeBSD 10)和相同版本的googleapis(7.1.0)上使用节点4.4.5。
执行下载的最终功能在这里。使用 get API从文件metadat中读取name
,auth
是从google.auth.OAuth2创建的auth对象,googleDrive
是google.drive的代理( 'v3')和google
来自google = require('googleapis'):
function googleDownload ( name, fileId, auth, downloadDirectory ) {
downloadDirectory = downloadDirectory || 'c:\\playground' ;
var targetFileName = path.join( downloadDirectory, name );
var dest = fs.createWriteStream(targetFileName);
console.log(`Starting download of ${fileId} as ${name}`);
googleDrive.files.get({
fileId: fileId,
auth: auth,
alt: 'media'
},
(err, response) => {
if(err) { console.log("Download error: ", err);}
else { console.log("Download completed."); }
})
.on('end', () => { console.log('All data received.'); })
.on('finish', () => { console.log('All data written.'); })
.on('close', () => { console.log('Connectin closed.'); })
.on('error', (err) => { console.log('Error during download: ', err); })
.pipe(dest);
}
通过查看代码,我无法在抛出错误的位置和实际应该发生的位置之间找到直接链接。我刚刚注意到googleapis包似乎复制了NodeJS API中可用的许多方法或函数,但无法说明对错误的影响。