如何使用 Google Drive V3 从共享文件夹中删除文件?
How do I delete files from a shared folder using Google Drive V3?
我正在尝试构建一个删除 shared Google 驱动器文件夹中的旧文件的功能,如下所示:
private async deleteExpiredFiles(
dataType: DataType,
folderId: string,
expirationDate: string,
): Promise<void> {
const {
data: { files: filesToDelete },
} = await this.drive.files.list({
q: `'${folderId}' in parents and createdTime <= '${expirationDate}'`,
supportsAllDrives: true,
includeItemsFromAllDrives: true,
});
if (!filesToDelete) {
return;
}
await Promise.all(
filesToDelete.map(async (file) => {
if (!file.id) {
return;
}
const permissionIds = await this.drive.permissions.list({
fileId: file.id,
supportsAllDrives: true,
});
if (!permissionIds.data.permissions) {
return;
}
Promise.all(
permissionIds.data.permissions.map(async (permission) => {
if (!permission.id || !file.id) {
return;
}
await this.drive.permissions.delete({
fileId: file.id,
permissionId: permission.id,
supportsAllDrives: true,
});
}),
);
}),
);
}
我已经在 Google Drive 文件夹中设置了所有权限,并为服务帐户获取了正确的凭据。但是,当我尝试 运行 时,出现以下错误:GaxiosError: The authenticated user does not have the required access to delete the permission.
我可以很好地使用 drive.files.list 并查看文件以及列出权限 ID。但它不允许我删除它们。有什么建议吗?
已解决!
而不是永久删除文件。我只需使用以下代码将它们移至垃圾箱:
await this.drive.files.update({
fileId: file.id,
requestBody: { trashed: true },
supportsAllDrives: true,
});
我正在尝试构建一个删除 shared Google 驱动器文件夹中的旧文件的功能,如下所示:
private async deleteExpiredFiles(
dataType: DataType,
folderId: string,
expirationDate: string,
): Promise<void> {
const {
data: { files: filesToDelete },
} = await this.drive.files.list({
q: `'${folderId}' in parents and createdTime <= '${expirationDate}'`,
supportsAllDrives: true,
includeItemsFromAllDrives: true,
});
if (!filesToDelete) {
return;
}
await Promise.all(
filesToDelete.map(async (file) => {
if (!file.id) {
return;
}
const permissionIds = await this.drive.permissions.list({
fileId: file.id,
supportsAllDrives: true,
});
if (!permissionIds.data.permissions) {
return;
}
Promise.all(
permissionIds.data.permissions.map(async (permission) => {
if (!permission.id || !file.id) {
return;
}
await this.drive.permissions.delete({
fileId: file.id,
permissionId: permission.id,
supportsAllDrives: true,
});
}),
);
}),
);
}
我已经在 Google Drive 文件夹中设置了所有权限,并为服务帐户获取了正确的凭据。但是,当我尝试 运行 时,出现以下错误:GaxiosError: The authenticated user does not have the required access to delete the permission.
我可以很好地使用 drive.files.list 并查看文件以及列出权限 ID。但它不允许我删除它们。有什么建议吗?
已解决!
而不是永久删除文件。我只需使用以下代码将它们移至垃圾箱:
await this.drive.files.update({
fileId: file.id,
requestBody: { trashed: true },
supportsAllDrives: true,
});