使用 NodeJS 获取 google 驱动器 API 中特定文件的内容
Get contents of a specific file in google drive API with NodeJS
我找到了很多关于如何使用 API 从 google 驱动器检索 .txt
文件内容的帖子。我试过使用这个:
const drive = google.drive({version: 'v3', auth});
var data = drive.files.get({
fileId: file_id,
alt: "media"
});
data.execute(function(response){
console.log(reponse)
})
我的错误
data.execute(function(response){
^
TypeError: data.execute is not a function
和 data.then
而不是 data.execute
每次出现错误时我都会研究并找到解决方法。有人可以给我一个如何从文件 ID 获取文件内容的更新版本吗?因为我认为以前的答案有些过时了。
抱歉,如果这很明显。一般来说,我对 javascript 和 api 比较陌生。所以这对我有很大帮助,因为这是我完成程序之前的最后阶段:)
谢谢,马蒂亚斯
当您 运行 'drive.files.get' 为 google 驱动器 API 时,您会收到一个承诺,并且要获取您必须使用的数据。它是这样工作的:
const filePath = `give_path_tosave_file`;
const dest = fs.createWriteStream(filePath);
let progress = 0;
drive.files.get(
{ fileId, alt: 'media' },
{ responseType: 'stream' }
).then(res => {
res.data
.on('end', () => {
console.log('Done downloading file.');
})
.on('error', err => {
console.error('Error downloading file.');
})
.on('data', d => {
d+='';
console.log(d);
//data will be here
// pipe it to write stream
}
})
.pipe(dest);
});
如果上述解决方案不起作用,您可以使用此解决方案。它在官方 google 网站上也是如此:
var fileId = '1ZdR3L3qP4Bkq8noWLJHSr_iBau0DNT4Kli4SxNc2YEo';
var dest = fs.createWriteStream('/tmp/filename.txt');
drive.files.export({
fileId: fileId,
mimeType: 'application/txt'
})
.on('end', function () {
console.log('Done');
})
.on('error', function (err) {
console.log('Error during download', err);
})
.pipe(dest);
有关更多信息,您应该查看 here。
此外,以下方法将 return 您有权访问的所有文件在驱动器中。
drive.files.list({}, (err, res) => {
if (err) throw err;
const files = res.data.files;
if (files.length) {
files.map((file) => {
console.log(file);
});
} else {
console.log('No files found');
}
});
我找到了很多关于如何使用 API 从 google 驱动器检索 .txt
文件内容的帖子。我试过使用这个:
const drive = google.drive({version: 'v3', auth});
var data = drive.files.get({
fileId: file_id,
alt: "media"
});
data.execute(function(response){
console.log(reponse)
})
我的错误
data.execute(function(response){
^
TypeError: data.execute is not a function
和 data.then
而不是 data.execute
每次出现错误时我都会研究并找到解决方法。有人可以给我一个如何从文件 ID 获取文件内容的更新版本吗?因为我认为以前的答案有些过时了。
抱歉,如果这很明显。一般来说,我对 javascript 和 api 比较陌生。所以这对我有很大帮助,因为这是我完成程序之前的最后阶段:)
谢谢,马蒂亚斯
当您 运行 'drive.files.get' 为 google 驱动器 API 时,您会收到一个承诺,并且要获取您必须使用的数据。它是这样工作的:
const filePath = `give_path_tosave_file`;
const dest = fs.createWriteStream(filePath);
let progress = 0;
drive.files.get(
{ fileId, alt: 'media' },
{ responseType: 'stream' }
).then(res => {
res.data
.on('end', () => {
console.log('Done downloading file.');
})
.on('error', err => {
console.error('Error downloading file.');
})
.on('data', d => {
d+='';
console.log(d);
//data will be here
// pipe it to write stream
}
})
.pipe(dest);
});
如果上述解决方案不起作用,您可以使用此解决方案。它在官方 google 网站上也是如此:
var fileId = '1ZdR3L3qP4Bkq8noWLJHSr_iBau0DNT4Kli4SxNc2YEo';
var dest = fs.createWriteStream('/tmp/filename.txt');
drive.files.export({
fileId: fileId,
mimeType: 'application/txt'
})
.on('end', function () {
console.log('Done');
})
.on('error', function (err) {
console.log('Error during download', err);
})
.pipe(dest);
有关更多信息,您应该查看 here。
此外,以下方法将 return 您有权访问的所有文件在驱动器中。
drive.files.list({}, (err, res) => {
if (err) throw err;
const files = res.data.files;
if (files.length) {
files.map((file) => {
console.log(file);
});
} else {
console.log('No files found');
}
});