为何新Google Service Account上传至共享文件夹报存储配额错误?
Google Drive API 服务账号上传文件权限问题
问题背景
我正尝试使用Node.js和服务账号通过Google Drive API,将文件上传至Google Drive中属于个人Google账号(非Workspace)的共享文件夹。已完成API配置:启用Google Drive API、创建服务账号、生成JSON密钥,并赋予该服务账号文件夹编辑权限。
第一个创建的服务账号可正常工作,但新创建的服务账号(同项目或不同项目)上传文件时会触发错误。
错误信息
Service Accounts do not have storage quota. Leverage shared drives, or use OAuth delegation instead. status: 403, code: 403, reason: 'storageQuotaExceeded'
已尝试操作
- 确认新服务账号拥有文件夹访问权限(可读取、列出文件);
- 同一服务账号多次使用密钥可正常工作;
- 未使用Shared Drives,仅使用个人账号共享文件夹。
代码示例
import { Injectable, InternalServerErrorException } from '@nestjs/common'; import { google } from 'googleapis'; import * as fs from 'fs'; import * as path from 'path'; import { Readable } from 'stream'; @Injectable() export class GoogleService { private drive; constructor() { const KEYFILEPATH = path.join(__dirname, '../../config/credentials.json'); const SCOPES = ['https://www.googleapis.com/auth/drive']; const auth = new google.auth.GoogleAuth({ keyFile: KEYFILEPATH, scopes: SCOPES, }); this.drive = google.drive({ version: 'v3', auth }); } async uploadFileToDrive(file: Express.Multer.File): Promise<string> { try { const fileMetaData = { name: file.originalname, parents: ['id-J9GMdeuPto'], // 共享文件夹ID }; const media = { mimeType: file.mimetype, //body: fs.createReadStream(file.path), body: Readable.from(file.buffer), }; const response = await this.drive.files.create({ requestBody: fileMetaData, media: media, fields: 'id', }); const fileId = response.data.id; // 设置文件公开可读 await this.drive.permissions.create({ fileId, requestBody: { role: 'reader', type: 'anyone', }, }); const url = `https://drive.google.com/file/d/${fileId}/view?usp=sharing`; return url; } catch (err) { console.error('上传Drive出错:', err); throw new InternalServerErrorException('上传文件至Drive失败'); } } }
疑问
为何首个服务账号正常,新创建的同配置账号报错?更换其他Google账号创建新服务账号仍存在相同问题。
内容的提问来源于stack exchange,提问作者Mauro Talamantes Villagrana Ki
相关产品推荐
相关产品推荐

