Node JS Google Drive api方法列表返回已删除的文件



我使用以下函数从Google Drive获取文件列表:

const { google } = require('googleapis');
const SCOPES = ['https://www.googleapis.com/auth/drive'];
const KETFILEPATH = "key.json"
const auth = new google.auth.GoogleAuth({
keyFile: KETFILEPATH,
scopes: SCOPES
})
let files_list = async (auth, nextPageToken = null, res = []) => {
const driveService = google.drive({ version: 'v3', auth })
const list = await driveService.files.list({
pageToken: nextPageToken,
fields: 'nextPageToken, files(id, name)',
});
const files = list.data.files;
if (files.length === 0) {
console.log('No files found.');
} else {
for (const file of files) {
res.push(file)
}
if (list.data.nextPageToken) {
return await files_list(auth, list.data.nextPageToken, res)
} else {
return res
}
}
}

但是这个函数返回了很多文件,我已经从https://drive.google.com/网站我也有空垃圾。更重要的是,当我尝试使用以下功能删除这些文件时:

const { google } = require('googleapis');
const SCOPES = ['https://www.googleapis.com/auth/drive'];
const KETFILEPATH = "key.json"
const auth = new google.auth.GoogleAuth({
keyFile: KETFILEPATH,
scopes: SCOPES
})
let deleteFile = async (auth, file_id) => {
const driveService = google.drive({ version: 'v3', auth })
let res = await driveService.files.delete({ 'fileId': file_id })
if (res.status != 200) {
console.log(res)
console.log("Error deleting file")
}
}

我得到这个输出:

{
config: {
url: 'https://www.googleapis.com/drive/v3/files/1Iddlq9Mri76rLhDi3TGCTYM-HEhVtEKL',
method: 'DELETE',
userAgentDirectives: [ [Object] ],
paramsSerializer: [Function (anonymous)],
headers: {
'x-goog-api-client': 'gdcl/5.0.2 gl-node/16.3.0 auth/7.2.0',
'Accept-Encoding': 'gzip',
'User-Agent': 'google-api-nodejs-client/5.0.2 (gzip)',
Authorization: '...',
Accept: 'application/json'
},
params: {},
validateStatus: [Function (anonymous)],
retry: true,
responseType: 'json'
},
data: '',
headers: {
'alt-svc': 'h3=":443"; ma=2592000,h3-29=":443"; ma=2592000,h3-T051=":443"; ma=2592000,h3-Q050=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000,quic=":443"; ma=2592000; v="46,43"',
'cache-control': 'no-cache, no-store, max-age=0, must-revalidate',
connection: 'close',
date: 'Wed, 07 Jul 2021 14:35:10 GMT',
expires: 'Mon, 01 Jan 1990 00:00:00 GMT',
pragma: 'no-cache',
server: 'GSE',
vary: 'Origin, X-Origin'
},
status: 204,
statusText: 'No Content',
request: {
responseURL: 'https://www.googleapis.com/drive/v3/files/1Iddlq9Mri76rLhDi3TGCTYM-HEhVtEKL'
}
}

我使用服务帐户进行身份验证。我已经和这个帐号分享了一个文件夹,他需要处理。

我该怎么处理?如何获取存储在Google Drive中的真实文件列表?

您已经与服务帐户共享了一个文件夹。但您正在请求服务帐户可以访问的所有文件。

const driveService = google.drive({ version: 'v3', auth })
const list = await driveService.files.list({
pageToken: nextPageToken,
fields: 'nextPageToken, files(id, name)',
});

您还没有添加父级,所以您不会只查看一个文件夹,而是请求所有文件。你有没有考虑过你看到的文件是服务帐户谷歌驱动器帐户上的文件?

const driveService = google.drive({ version: 'v3', auth })
const list = await driveService.files.list({
pageToken: nextPageToken,
q: `'${FolderId}' in parents`,
fields: 'nextPageToken, files(id, name)',
});

您必须使用以下

let result = await drive.files.list({
q: "mimeType='application/vnd.google-apps.folder' and trashed=false",
fields: 'nextPageToken, files(id, name)',
spaces: 'drive',
});

在此情况下,thrashed=false不会检查Thrash中的文件。所有最好的

您需要使用includeRemoved:false

const driveService = google.drive({ version: 'v3', auth })
const list = await driveService.files.list({
pageToken: nextPageToken,
includeRemoved: false,
q: `'${FolderId}' in parents`
fields: 'nextPageToken, files(id, name)',
});

相关内容

  • 没有找到相关文章

最新更新