Search code examples
node.jsgoogle-apigoogle-drive-apiservice-accountsgoogle-api-nodejs-client

Node JS Google Drive api error: The user's Drive storage quota has been exceeded


I want to upload files to Google Drive using Node JS api. I have enabled Google Drive api and created service account. Then I share one folder with this account. The problem is that I see file I have uploaded using node js, but free space doesn't change when I upload files using api, so I can't monitor how many space left. What is more, when I upload about 7 GB using api this error appered(I have 14 GB free on Google Drive):

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

Why can I see this files on Google Drive, but they don't use my Google Drive space? How can I toogle it to use my Google Drive space?

Uploading function:


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
    }
}

Solution

  • Issue:

    As you can see at Share folders in Google Drive, the storage space will be occupied by the account who uploaded the file (that is, the owner of the file), not the owner of the shared folder:

    Storage is counted against the person who uploaded the file, not the owner of the folder.

    Therefore, if you try to upload the file with the service account, the file will take storage space in the service account's Drive. That is to say, when you check there are still 14 GB free in your regular account's Drive, you are looking at the wrong place.

    Possible solutions:

    Call About: get with your service account to check how much space you have left in that account's Drive. Chances are you can delete some files in order to free up some storage space.

    If that is not a possibility, I'd suggest granting domain-wide authority to the service account and use that to impersonate your regular account (and upload the files on behalf of it).

    Of course, transfering ownership of the file would free up space in the service account's Drive, but since you cannot upload the file, I don't think that is a feasible solution to this problem.