gpt4 book ai didi

javascript - NodeJS - 从 Google Drive 下载

转载 作者:太空宇宙 更新时间:2023-11-04 00:41:55 26 4
gpt4 key购买 nike

我有这段代码,用于从 Google 云端硬盘下载文件:

function downloadDrive(fileId, callback) {
var fileExt = fileId.split(".");
var file = Date.now() + "." + fileExt[fileExt.length - 1];
var dest = fs.createWriteStream("./files/"+file);
service.files.get({
auth: oauth2Client,
fileId: fileExt[0],
alt: "media"
})
.on("finish", function() {
callback(file);
})
.on("error", function(err) {
console.log("Error during download", err);
})
.pipe(dest);
}

它在处理 ~500Mb 的小文件时效果非常好。但是,当尝试下载相当大的 gzip 文件 ~3Gb 时,会抛出以下错误。

buffer.js:23
const ui8 = new Uint8Array(size);
^

RangeError: Invalid typed array length
at new Uint8Array (native)
at createBuffer (buffer.js:23:15)
at allocate (buffer.js:98:12)
at new Buffer (buffer.js:53:12)
at Function.Buffer.concat (buffer.js:225:16)
at BufferList.copy (/Synology/server_Metagenomics/server/node_modules/googleapis/node_modules/google-auth-library/node_modules/request/node_modules/bl/bl.js:124:21)
at BufferList.slice (/Synology/server_Metagenomics/server/node_modules/googleapis/node_modules/google-auth-library/node_modules/request/node_modules/bl/bl.js:99:15)
at BufferList.toString (/Synology/server_Metagenomics/server/node_modules/googleapis/node_modules/google-auth-library/node_modules/request/node_modules/bl/bl.js:166:15)
at Request.<anonymous> (/Synology/server_Metagenomics/server/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)

我没有找到很多相关信息。这是怎么回事?

最佳答案

您可以尝试以下方式下载文件。该错误似乎是因为您正在下载大文件。

var filename = 'zzz.txt';
var proxyUrl = "http://" + user + ":" + password + "@" + host + ":" + port;
var token = 'YOUR_TOKEN';

var req = request.get('https://www.googleapis.com/drive/v3/files/YOUR_FILE_ID?alt=media', {
'auth': {
'bearer': token
},
'proxy': proxyUrl
}).on('response', function(res) {

// create file write stream
var fws = fs.createWriteStream(filename);

// setup piping
res.pipe(fws);

res.on('err', function() {
console.log("error occured.....");
});

res.on('end', function() {
console.log('Done');
// go on with processing
});
});

关于javascript - NodeJS - 从 Google Drive 下载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36288309/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com