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

使用Google Apps Script获取Dropbox文件夹子目录中的文件

整合Dropbox list_folder/continue 接口实现完整文件列表获取

当调用Dropbox的list_folder接口时,如果目标文件夹内的文件(含子目录文件)数量超过设置的limit(你的脚本里是2000),接口会返回has_more: true,同时给出一个cursor参数——这时候就需要用list_folder/continue接口来获取剩余的文件数据。以下是修改后的完整脚本,整合了分页处理逻辑:

function myFunction() {
  const accessToken = "###"; // 替换为你的Dropbox access token
  const path = "/SampleName";

  let allEntries = [];
  let cursor = null;
  let hasMore = true;

  // 第一次调用list_folder接口获取初始数据
  const initialUrl = "https://api.dropboxapi.com/2/files/list_folder";
  let res = UrlFetchApp.fetch(initialUrl, {
    method: "post",
    headers: { authorization: "Bearer " + accessToken },
    contentType: "application/json",
    payload: JSON.stringify({ path, limit: 2000, recursive: true })
  });
  let obj = JSON.parse(res.getContentText());
  allEntries = [...allEntries, ...obj.entries];
  cursor = obj.cursor;
  hasMore = obj.has_more;

  // 循环调用continue接口获取剩余分页数据
  const continueUrl = "https://api.dropboxapi.com/2/files/list_folder/continue";
  while (hasMore) {
    res = UrlFetchApp.fetch(continueUrl, {
      method: "post",
      headers: { authorization: "Bearer " + accessToken },
      contentType: "application/json",
      payload: JSON.stringify({ cursor: cursor })
    });
    obj = JSON.parse(res.getContentText());
    allEntries = [...allEntries, ...obj.entries];
    cursor = obj.cursor;
    hasMore = obj.has_more;
  }

  // 输出所有获取到的文件条目
  console.log(allEntries);
}

关键逻辑说明

  • allEntries数组:专门用来存储所有分页返回的文件条目,每次调用接口后会将新返回的entries合并进去,最终得到完整的文件列表。
  • cursor参数:每一次接口返回的cursor是下一页数据的唯一凭证,调用continue接口时必须传递该参数,才能获取到剩余的文件数据。
  • 循环终止条件:当接口返回的has_more变为false时,说明所有文件数据已经全部获取完成,此时终止循环。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.11 06:04:58