I believe your goal as follows.
- You want to retrieve the file list using Drive API with googleapis for Node.js
- In your environment, you have the shared Drive, and you want to retrieve the file list not only your Google Drive, but also the shared Drive.
- You have already been able to retrieve the file list using Drive API.
Modification points:
- In order to retrieve the file list from both your own drive and the shared drive, it is required to set the several specific parameters for the query parameter of the endpoint. This has already been mentioned by Ron M's answer.
- From your question,
- I thought the possibility that you have several shared Drives.
- For this,
allDrives
is used for corpora
. By this, it is not required to be set each drive ID.
- You might have the files of more than 1000 files.
- For this, the file list is retrieved using
NextPageToken
.
When above points are reflected to your script, it becomes as follows.
Modified script:
When you want to retrieve the file list from both your drive and the shared drive, you can use the following script. In this case, even when you have several shared Drives, all file list can be retrieved from all shared drives and your drive.
const drive = google.drive({ version: "v3", auth });
const fileList = [];
let NextPageToken = "";
do {
const params = {
pageToken: NextPageToken || "",
pageSize: 1000,
fields: "nextPageToken, files(id, name)",
corpora: "allDrives",
includeItemsFromAllDrives: true,
supportsAllDrives: true,
};
const res = await drive.files.list(params);
Array.prototype.push.apply(fileList, res.data.files);
NextPageToken = res.data.nextPageToken;
} while (NextPageToken);
console.log(fileList);
Note:
If you want to split the file list for your drive and shared drives, you can also use the following script.
const drive = google.drive({ version: "v3", auth });
const fileList = { myDrive: [], sharedDrives: {} };
let NextPageToken = "";
do {
const params = {
pageToken: NextPageToken || "",
pageSize: 1000,
fields: "nextPageToken, files(id, name, driveId)",
corpora: "allDrives",
includeItemsFromAllDrives: true,
supportsAllDrives: true,
};
const res = await drive.files.list(params);
res.data.files.forEach((f) => {
if (f.driveId) {
fileList.sharedDrives[f.driveId] = fileList.sharedDrives[f.driveId]
? fileList.sharedDrives[f.driveId].concat(f)
: [f];
} else {
fileList.myDrive = fileList.myDrive.concat(f);
}
});
NextPageToken = res.data.nextPageToken;
} while (NextPageToken);
console.log(fileList);
Reference:
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…