使用Google Drive Service Account遇403存储配额错误求助
问题:Google Drive Service Account 403 存储配额错误(实际空间未超)
我使用Google Drive Service Account(作为编辑者)通过Google Drive API共享文件,初始运行正常。一日后收到403错误提示无存储配额,创建新Service Account并以所有者身份上传后恢复正常。今日再次上传时重复出现相同403错误,但我的Google Drive仅使用了不足50K存储空间。
代码实现
query = f"'{dirId}' in parents and name = '{fName}' and trashed = false" # checking already-existing files. response = self.__service.files().list( q=query, corpora="user", fields="files(id, name, createdTime, modifiedTime, modifiedByMeTime, viewedByMeTime)" ).execute() # If find old files with the same names, delete them. for gFile in response.get('files', []): gFileId = gFile.get('id') gFileName = gFile.get('name') self.__service.files().update(removeParents=dirId, fileId=gFileId).execute() print(f"Deleted files with the same name from Google Drive: {fPath}/{gFileName} (ID: {gFileId})") # Now upload the file with same name. fileMetadata = { 'name': fName, 'parents': [dirId] } media = MediaFileUpload(fPath, resumable=True) try: uploaded_file = self.__service.files().create( body=fileMetadata, media_body=media, fields='id', supportsAllDrives=True ).execute() print(f"Done uploading {fPath}*{fName}, ID: {uploaded_file.get('id')}") except Exception as ex: TestDragonCommons.printEx(ex, "???") print ("^^^")
错误信息
<HttpError 403 when requesting None returned "Service Accounts do not have storage quota. Leverage shared drives (https://developers.google.com/workspace/drive/api/guides/about-shareddrives), or use OAuth delegation (http://support.google.com/a/answer/7281227) instead.". Details: "[{'message': 'Service Accounts do not have storage quota. Leverage shared drives (https://developers.google.com/workspace/drive/api/guides/about-shareddrives), or use OAuth delegation (http://support.google.com/a/answer/7281227) instead.', 'domain': 'usageLimits', 'reason': 'storageQuotaExceeded'}]">
解决建议
- 核心原因:Service Account本身没有独立的存储配额,它依赖关联的Google Workspace账户或共享驱动器的配额。之前新建SA恢复正常只是临时权限同步效果,未解决根本问题。
- 方案1:切换到共享驱动器
- 创建共享驱动器,将Service Account添加为成员(至少编辑权限)
- 上传时将文件
parents设为共享驱动器ID,保持supportsAllDrives=True
- 方案2:配置OAuth域范围委托
- 若使用Google Workspace,给Service Account配置域范围委托,让它模拟域内用户操作,使用该用户的Drive配额
- 代码优化:
- 当前删除逻辑仅移除文件父文件夹,未真正删除,文件仍可能占用配额。应调用
files().delete(fileId=gFileId)彻底删除,或移至回收站后清空 - 若操作共享驱动器,查询时
corpora参数需设为drive而非user
- 当前删除逻辑仅移除文件父文件夹,未真正删除,文件仍可能占用配额。应调用
内容的提问来源于stack exchange,提问作者Farn Wang
相关产品推荐
相关产品推荐

