【发布时间】:2021-07-09 16:54:50
【问题描述】:
我想使用 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 云端硬盘上看到这些文件,但它们不使用我的 Google 云端硬盘空间?如何调整它以使用我的 Google 云端硬盘空间?
上传功能:
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
}
}
【问题讨论】:
-
您将文件上传到哪个帐户?到您的常规帐户还是到您的服务帐户?如果是后者,请使用服务帐号拨打About: get查看您还剩多少空间。
-
@DaImTo 不幸的是,我找不到使用 Node JS api 使用此方法的方法
-
@lamblichus 我是对的,如果我将文件上传到文件夹,我在我的常规帐户中创建并与服务帐户共享,我将文件上传到常规帐户吗? (我在上传时指定文件夹ID)
-
是的,您需要更新文件的权限以使您的个人帐户成为所有者。 developers.google.com/drive/api/v3/reference/permissions/update检查转让所有权
标签: node.js google-api google-drive-api service-accounts google-api-nodejs-client