用于复制整个 Google Drive 文件结构的 Google Apps 脚本;如何避免超时? [英] Google Apps Script To Copy Entire Google Drive File Structure; How To Avoid Timeouts?

查看:20
本文介绍了用于复制整个 Google Drive 文件结构的 Google Apps 脚本;如何避免超时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的单位正在切换到 Google 企业帐户,每个人都需要将他们的云端硬盘文件转移到他们的新帐户.Drive 不允许在这些帐户之间转移所有权,因此我创建了一个脚本来将文件和文件夹从旧帐户复制到新帐户.(旧帐户的内容已移至与新帐户共享的文件夹中.)

My organization is switching to a Google Business account, and everyone needs to transfer their Drive files to their new accounts. Drive will not allow transfer of ownership between these accounts, so I've created a script to copy files and folders from the old account to the new account. (The old account's contents have been moved into a folder shared with the new account.)

这是我目前所拥有的:

function copyDrive() {
  var originFolder = DriveApp.getFolderById(originFolderID);
  var destination = DriveApp.getFolderById(destinationID);
  copyFiles(originFolder, destination);

};

function copyFiles(passedFolder, targetFolder) {
  var fileContents = passedFolder.getFiles();
  var file;
  var fileName;

  while(fileContents.hasNext()) {
    file = fileContents.next();
    fileName = file.getName();
    file.makeCopy(fileName, targetFolder);
  }
  copySubFolders(passedFolder, targetFolder);
};

function copySubFolders(passedFolder, targetFolder) {
  var folderContents = passedFolder.getFolders();
  var folder;
  var folderName;

  while(folderContents.hasNext()) {
    folder = folderContents.next();
    folderName = folder.getName();
    var subFolderCopy = targetFolder.createFolder(folderName);
    copyFiles(folder, subFolderCopy);
  }
};

请原谅任何不雅;我对此很陌生.该脚本实际上运行良好并保留了文件夹结构,但在复制约 150 个文件和文件夹后会超时.我一直在研究如何使用延续令牌,并且我已经阅读了 这篇文章 密切.我认为我停留在概念层面,因为我不确定延续标记将如何与我设置的递归函数交互.看起来我最终会得到一堆我的 copySubFolders 函数,它们每个都需要自己的延续令牌.当然,它们的迭代器都使用相同的变量名,所以我真的不知道如何设置它.

Please pardon any inelegance; I am new at this. The script actually works great and preserves the folder structure, but it times out after copying ~150 files and folders. I've been looking into how to use continuation tokens, and I've read this post closely. I think I'm stuck on a conceptual level, because I'm not sure how the continuation tokens will interact with the recursive functions I've set up. It seems like I will end up with a stack of my copySubFolders function, and they will each need their own continuation tokens. Of course they all use the same variable name for their iterators, so I really have no idea how to set that up.

有什么想法吗?很抱歉发布这样一个无助的新手问题;我希望这至少对某人来说是一个有趣的问题.

Any thoughts? Sorry for posting such a helpless newbie question; I hope it will at least be an interesting problem for someone.

推荐答案

我想我已经解决了概念问题,虽然我得到了

I think I have solved the conceptual problem, though I am getting

很抱歉,发生服务器错误.请稍等,然后重试.(第 9 行,文件代码")

We're sorry, a server error occurred. Please wait a bit and try again. (line 9, file "Code")

当我尝试执行它时.

基本上,我将其设置为一次仅尝试复制一个顶级文件夹,并且对于每个顶级文件夹,它都使用我之前拥有的递归函数.它应该为第一级文件夹和根文件夹中的任何文件保存延续令牌,以便它可以在下一次执行中从它停止的地方继续.这样一来,我的递归函数堆栈中就不会涉及到令牌.

Basically, I set it up to only try to copy one top-level folder at a time, and for each one of those it uses the recursive functions I had before. It should save continuation tokens for that first level of folders and any files in the root folder so it can pick up in the next execution where it left off. This way, the tokens are not involved in my recursive stack of functions.

function copyDrive() {


  var originFolder = DriveApp.getFolderById(originFolderID);
  var destination = DriveApp.getFolderById(destinationID);

  var scriptProperties = PropertiesService.getScriptProperties();
  var fileContinuationToken = scriptProperties.getProperty('FILE_CONTINUATION_TOKEN');
  var fileIterator = fileContinuationToken == null ?
    originFolder.getFiles() : DriveApp.continueFileIterator(fileContinuationToken);
  var folderContinuationToken = scriptProperties.getProperty('FOLDER_CONTINUATION_TOKEN');
  var folderIterator = folderContinuationToken == null ?
    originFolder.getFolders() : DriveApp.continueFolderIterator(folderContinuationToken);

  try {
    var rootFileName;
    while(fileIterator.hasNext()) {
      var rootFile = fileIterator.next();
      rootFileName = rootFile.getName();
      rootFile.makeCopy(rootFileName, destination);
      }

    var folder = folderIterator.next();
    var folderName = folder.getName();
    var folderCopy = folder.makeCopy(folderName, destination);


    copyFiles(folder, folderCopy);

  } catch(err) {
    Logger.log(err);
  }

  if(fileIterator.hasNext()) {
    scriptProperties.setProperty('FILE_CONTINUATION_TOKEN', fileIterator.getContinuationToken());
  } else {
    scriptProperties.deleteProperty('FILE_CONTINUATION_TOKEN');
  }
  if(folderIterator.hasNext()) {
    scriptProperties.setProperty('FOLDER_CONTINUATION_TOKEN', folderIterator.getContinuationToken());
  } else {
    scriptProperties.deleteProperty('FOLDER_CONTINUATION_TOKEN');
  }

};

function copyFiles(passedFolder, targetFolder) {
  var fileContents = passedFolder.getFiles();
  var file;
  var fileName;

  while(fileContents.hasNext()) {
    file = fileContents.next();
    fileName = file.getName();
    file.makeCopy(fileName, targetFolder);
  }
  copySubFolders(passedFolder, targetFolder);
};

function copySubFolders(passedFolder, targetFolder) {
  var subFolderContents = passedFolder.getFolders();
  var subFolder;
  var subFolderName;

  while(folderContents.hasNext()) {
    subFolder = subFolderContents.next();
    subFolderName = subFolder.getName();
    var subFolderCopy = targetFolder.createFolder(folderName);
    copyFiles(subFolder, subFolderCopy);
  }
};

这篇关于用于复制整个 Google Drive 文件结构的 Google Apps 脚本;如何避免超时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆