首页 > 解决方案 > Node JS Google Drive api错误:已超出用户的Drive存储配额

问题描述

我想使用 Node JS api 将文件上传到 Google Drive。我已启用 Google Drive api 并创建了服务帐户。然后我与这个帐户共享一个文件夹。问题是我看到我使用node js上传的文件,但是当我使用api上传文件时可用空间没有改变,所以我无法监控剩余多少空间。更重要的是,当我使用 api 上传大约 7 GB 时出现此错误(我在 Google Drive 上有 14 GB 可用空间):

code: 403,
  errors: [
    {
      domain: 'global',
      reason: 'storageQuotaExceeded',
      message: "The user's Drive storage quota has been exceeded."
    }
  ]

为什么我可以在 Google Drive 上看到这些文件,但它们不使用我的 Google Drive 空间?我怎样才能使用它来使用我的 Google Drive 空间?

上传功能:


const { google } = require('googleapis');
const path = require('path');
const fs = require('fs');

const SCOPES = ['https://www.googleapis.com/auth/drive'];

const KETFILEPATH = "key.json"


let main_dir_id = "1oT2Fxi1L6iHl9pDNGyqwBDyUHyHUmCJJ"


const auth = new google.auth.GoogleAuth({
    keyFile: KETFILEPATH,
    scopes: SCOPES
})
let createAndUploadFile = async (auth, file_path, mimeType, folder_id, i = 0) => {
    const driveService = google.drive({ version: 'v3', auth })

    let fileMetaData = {
        'name': file_path.slice(file_path.lastIndexOf("/") + 1),
        'parents': [folder_id]
    }
    let media = {
        mimeType: mimeType,
        body: fs.createReadStream(file_path)
    }
    let res = await driveService.files.create({
        resource: fileMetaData,
        media: media,
    })
    if (res.status === 200) {
        console.log('Created file id: ', res.data.id)
        return res.data.id
    } else {
        // this error is in res
        return 0
    }
}

标签: node.jsgoogle-apigoogle-drive-apiservice-accountsgoogle-api-nodejs-client

解决方案


问题:

在 Google Drive 中的共享文件夹中可以看到,存储空间将被上传文件的帐户(即文件的所有者)占用,而不是共享文件夹的所有者:

存储空间是针对上传文件的人计算的,而不是文件夹的所有者。

因此,如果您尝试使用服务帐户上传文件,该文件将占用服务帐户驱动器中的存储空间。也就是说,当您检查常规帐户的 Drive 中仍有 14 GB 可用空间时,您看错了地方。

可能的解决方案:

致电关于:使用您的服务帐户检查您在该帐户的云端硬盘中剩余的空间。您可以删除一些文件以释放一些存储空间。

如果这不可能,我建议向服务帐户授予域范围的权限,并使用它来模拟您的常规帐户(并代表它上传文件)。

当然,转移文件的所有权会释放服务帐户驱动器中的空间,但由于您无法上传文件,我认为这不是解决此问题的可行方案。


推荐阅读