google-drive-api - 将大量 Google Drive 文件转移到另一个 Drive 帐户?
问题描述
如何将大量视频文件(约 400 GB 的篮球比赛影片)从一个帐户传输到另一个帐户?我不能只使用“更改所有权”工具,因为这是从账户 A(我的学校账户)到账户 B(我的个人账户)。我可以访问 Google Takeout 工具,但我不认为我可以从那里从一个 Google Drive 转移到另一个帐户,而无需在中间使用另一个平台并且需要完整下载内容并重新上传,这不切实际许多大文件。
(在担心之前,所有这些数据都是我创建的数据,我负责;不是受版权保护/盗版的素材。)
解决方案
我相信你的情况和目标如下。
- 您有权复制源 Google Drive 中的所有文件。
- 这些文件放在特定文件夹中,包括嵌套文件夹。
- 您的 Google Drive(目标文件夹端)有足够的存储空间来复制所有文件。
- 您想实现使用脚本复制所有文件。
为此,这个答案怎么样?我认为您的目标可以使用 Google Apps 脚本来实现。该示例脚本的流程如下。
- 从特定源文件夹(包括嵌套文件夹)中检索所有文件 ID。
- 从检索到的文件 ID 创建批处理请求。
- 使用 Drive API 的 files.copy 方法和批处理请求将文件复制到 Google Drive 中的特定文件夹。
用法:
请执行以下流程。
1. 新建 Google Apps Script 项目。
Web Apps 的示例脚本是 Google Apps 脚本。所以请创建一个 Google Apps Script 项目。
如果要直接创建,请访问https://script.new/。在这种情况下,如果您没有登录 Google,则会打开登录屏幕。所以请登录谷歌。至此,Google Apps Script 的脚本编辑器被打开。
2. 安装 Google Apps 脚本库。
在此答案中,使用了 Google Apps 脚本库。它是BatchRequest。通过使用此库,可以使用异步过程完成文件复制。这样,与同步处理相比,处理成本可以降低一点。
关于安装库的方法,请查看这里。
3.启用驱动API。
请在高级 Google 服务中启用 Drive API。这样,Drive API 会在 API 控制台自动启用。在此示例脚本中,使用了 Drive API v3。
4. 示例脚本。
请复制并粘贴以下脚本。并将源文件夹 ID 设置为sourceFolderId
. 在您的情况下,顶部文件夹 ID 为They are in several folders, nested inside of one main folder.
. 另外,请将目标文件夹 ID 设置为destinationFolderId
. 在这种情况下,请在您的 Google Drive 中设置文件夹 ID。
function myFunction() {
const sourceFolderId = "###"; // Please set the source folder ID.
const destinationFolderId = "###"; // Please set the destination folder ID.
const getFiles = (id, res = []) => {
const folder = DriveApp.getFolderById(id);
const files = folder.getFiles();
while (files.hasNext()) {
const file = files.next();
res.push({name: file.getName(), id: file.getId()})
}
let ids = [];
const folders = folder.getFolders();
while (folders.hasNext()) ids.push(folders.next().getId());
if (ids.length > 0) ids.forEach(id => getFiles(id, res));
return res;
}
const files = getFiles(sourceFolderId);
const limit = 100;
const split = Math.ceil(files.length / limit);
for (let i = 0; i < split; i++) {
const batches = files.splice(0, limit).map(f => ({
method: "POST",
endpoint: `https://www.googleapis.com/drive/v3/files/${f.id}/copy?supportsAllDrives=true`,
requestBody: {name: f.name, parents: [destinationFolderId]},
}));
const requests = {batchPath: "batch/drive/v3", requests: batches};
const result = BatchRequest.Do(requests);
console.log(result.getContentText());
}
// DriveApp.createFile() // This comment line is used for automatically detecting the scope of `https://www.googleapis.com/auth/drive` with the script editor. So please don't remove this line.
}
- 当您在脚本编辑器中运行此功能时,将打开授权屏幕。所以请授权范围。这样,脚本就运行了。
笔记:
- 在这种情况下,所有文件都将复制到
destinationFolderId
. - 请在启用 V8 的情况下使用此脚本。
- 如果您是源账户的所有者,我认为如果顶层文件夹的父文件夹可以直接更改为您的账户的文件夹,即目标文件夹,则流程成本将最低。但是在我的环境中,我无法测试它。因此,如果数据被删除,这是个大问题。所以我提出了复制所有文件的方法。
参考:
推荐阅读
- c++ - 在现代 C++ 中在函数声明中定义返回变量是否合法?
- python - 如何使用报纸从文本文件中的 URL 列表中提取报纸文章
- reactjs - React with TypeScript:如何在新组件中访问 HOC 方法
- ajax - Laravel:使用 Ajax 和 Jquery 从选择选项中获取值
- c# - 下载存储的 PDF 会损坏
- flutter - Flutter:StreamBuilder 的行为类似于 StatelessWidget
- ruby-on-rails - Rails:ActiveSupport::MessageEncryptor::InvalidMessage
- msbuild - 如何让 MSBuild 作用于文件夹列表
- java - java.lang.NoClassDefFoundError: org/apache/poi/ss/usermodel/Workbook 问题
- uwp - UWP + DirectX11:鼠标移动