You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Google Shared Drive文件数量临近上限提醒及利用率查询方案咨询

Google Shared Drive 文件上限监控与告警解决方案

核心实现:基于Google Drive API + Google Apps Script(无额外成本)

无需依赖IT部门的Admin Panel,直接通过API获取文件统计,实现自动告警或管理员自助查询。

1. 关键前置准备

  • 获取目标Shared Drive的driveId:可从Shared Drive的网页URL中提取(格式类似https://drive.google.com/drive/u/0/folders/[driveId]),或通过Drive API的drives.list接口查询
  • 授权范围:仅需https://www.googleapis.com/auth/drive.readonly只读权限即可

2. 自动告警脚本(定时触发)

用Google Apps Script部署定时任务,当文件数达到上限95%(380000)时自动发送邮件通知管理员:

function checkSharedDriveQuota() {
  const TARGET_DRIVE_ID = "替换为你的Shared Drive ID";
  const QUOTA_LIMIT = 400000;
  const ALERT_THRESHOLD = 0.95;
  const ADMIN_EMAILS = ["admin@yourdomain.com", "backup-admin@yourdomain.com"];

  // 分页获取文件总数
  let totalFiles = 0;
  let pageToken = null;
  do {
    const response = Drive.Files.list({
      corpora: "drive",
      driveId: TARGET_DRIVE_ID,
      includeItemsFromAllDrives: true,
      supportsAllDrives: true,
      pageToken: pageToken,
      pageSize: 1000
    });
    totalFiles += response.items ? response.items.length : 0;
    pageToken = response.nextPageToken;
  } while (pageToken);

  // 触发告警逻辑
  if (totalFiles >= QUOTA_LIMIT * ALERT_THRESHOLD) {
    const usageRate = ((totalFiles / QUOTA_LIMIT) * 100).toFixed(2);
    const subject = `⚠️ Shared Drive 容量告警:已达${usageRate}%上限`;
    const body = `目标Shared Drive当前文件数:${totalFiles}\n上限:${QUOTA_LIMIT}\n请及时清理冗余文件或申请扩容,避免影响工作。`;
    MailApp.sendEmail(ADMIN_EMAILS.join(","), subject, body);
  }
}

部署步骤:

  • 打开Google Apps Script新建项目
  • 粘贴代码并替换参数(TARGET_DRIVE_ID、ADMIN_EMAILS)
  • 点击「资源」→「高级Google服务」,启用Drive API
  • 添加定时触发器:「编辑」→「当前项目的触发器」,设置执行频率(如每天上午9点)
  • 首次运行时完成权限授权(需访问Drive和发送邮件)

3. 管理员自助查询页面

部署Web应用,让管理员随时查看利用率,无需IT介入:

function doGet() {
  const TARGET_DRIVE_ID = "替换为你的Shared Drive ID";
  const QUOTA_LIMIT = 400000;

  // 获取文件总数
  let totalFiles = 0;
  let pageToken = null;
  do {
    const response = Drive.Files.list({
      corpora: "drive",
      driveId: TARGET_DRIVE_ID,
      includeItemsFromAllDrives: true,
      supportsAllDrives: true,
      pageToken: pageToken,
      pageSize: 1000
    });
    totalFiles += response.items ? response.items.length : 0;
    pageToken = response.nextPageToken;
  } while (pageToken);

  const usageRate = ((totalFiles / QUOTA_LIMIT) * 100).toFixed(2);
  const statusMsg = usageRate >= 95 ? "<strong>⚠️ 已接近上限,请及时处理</strong>" : "当前状态正常";
  
  return HtmlService.createHtmlOutput(`
    <html>
      <body style="font-family: Arial, sans-serif; max-width: 600px; margin: 2rem auto; padding: 0 1rem;">
        <h2>Shared Drive 利用率查询</h2>
        <hr>
        <p>当前文件总数:<strong>${totalFiles}</strong></p>
        <p>利用率:<strong>${usageRate}%</strong>(上限:${QUOTA_LIMIT})</p>
        <p>${statusMsg}</p>
      </body>
    </html>
  `);
}

部署步骤:

  • 在同一Apps Script项目中添加上述函数
  • 点击「发布」→「部署为Web应用」,设置:
    • 执行权限:「任何人,甚至匿名」(或限制为组织内用户)
    • 访问权限:按需选择
  • 将生成的URL发给管理员,即可随时打开查看

4. 权限注意事项

  • 脚本执行账号只需拥有目标Shared Drive的只读权限,无需IT管理员权限
  • 若需长期稳定运行,建议使用组织内的服务账号授权,避免个人账号离职导致脚本失效

内容的提问来源于stack exchange,提问作者Svetlozar Draganov

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.27 17:12:46