使用Google Drive API转移所有用户文件所有权的最有效流程 [英] Most efficient process for transferring ownership of all of a user's files using the Google Drive API

查看:835
本文介绍了使用Google Drive API转移所有用户文件所有权的最有效流程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了Google Apps脚本功能,该功能使用域名范围授权 (Oauth 2服务帐户)以及 Google Drive API 转让所有Google的所有权将用户(用户A)拥有的文件驱动到其他用户(用户B),然后在所有文件中添加第三个用户(用户C)作为编辑器。



我这样做的原因是因为我想在员工离开公司并为任何人处理他们的责任时为他们提供访问权限时自动保存文件的过程。
$ b strong>用户B 是一个Google帐户,专门用于存放已离开公司的用户的
文件,

  • 用户C strong>是负责接管用户A的
    职责的人。



  • Google提供了转让所有权工具,但无法通过脚本作为应用程序管理员的一部分调用它自动化过程,只能通过管理控制台GUI进行。

    我已经能够使用API​​成功执行此过程的每个步骤,但每个文件需要几次API调用。因此,如果用户拥有数百或数千个文件,则其功能可能会超过5分钟的脚本执行限制。

    有没有一种方法可以合并或删除这些步骤,并且仍然可以实现相同的目标?



    以下是我提出的过程:


    1. 以用户A的身份进行身份验证

    2. 使用文件:列表,列出用户A的所有驱动器文件,这些文件归他们所有并且不在垃圾箱中 每个页面的结果和存储文件的详细信息(ID,标题,父母等)在一个数组
    3. 使用文件:插入,创建一个最终包含用户A所有文件和文件夹的根级容器文件夹,用于组织目的(称为来自用户A(日期))


    4. 循环遍历文件详细信息数组中的每个文件如果文件的父目录是根目录将其父级设置为容器文件夹(使用文件:补丁)。这会将它们拥有的所有文件移动到容器文件夹中,但保持子文件夹/文件的目录结构不变。

    5. 使用权限:插入,将用户B设置为容器文件夹的所有者。这使容器文件夹显示在用户B的驱动器的根目录中。但是,这不会将Owner权限级联到容器文件夹中包含的所有文件/文件夹。 使用权限:插入,将用户C设置为容器文件夹的编辑器,以便该文件夹显示在其与我共享文件夹。
    6. 再次循环遍历文件详情数组中的每个文件:


      1. 使用权限:插入 ,将用户B设置为每个文件的所有者

      2. 使用权限:插入,将用户C设置为编辑器(使用sendNotificationEmails = false参数,因此用户C不会被数千封通知电子邮件充斥)



    7. 认证为用户B,再次遍历文件详情数组中的每个文件,并且:步骤8-1导致每个文件显示(当你点击每个文件时,它显示两个父文件夹:容器文件夹和我的驱动器根文件夹),所以现在从每个文件的父数组中删除根文件夹(使用文件:补丁


    8. 使用权限:删除,移除用户A的权限(因为他们的帐户将设置为Vault Former Employee,这会导致他们仍然显示在Google云端硬盘共享窗格中,但他们不应该因为他们不再在公司中)

    9. 刚刚发布的Google a Data Transfer API ,它可以完成大部分工作。它不处理与用户C相关的部分,但是它排除了最长时间运行的大部分步骤。

      I have written a Google Apps Script function that uses Domain-Wide Delegation of Authority (Oauth 2 service account), and the Google Drive API to transfer ownership of all Google Drive files owned by a user (User A) to another user (User B) and then adds a third user (User C) as an editor on all of the files.

      The reason I am doing this is because I want to automate the process of preserving files when an employee leaves the company and providing access to them for whomever will be handling their responsibilities.

      • User A is the person leaving the company,
      • User B is a Google account meant specifically to be an archive of files from people who have left the company,
      • User C is the person who is responsible for taking over User A’s responsibilities.

      Google provides a Transfer Ownership tool for Apps admins, but it can not be invoked through a script as part of an automated process, only through the Admin Console GUI.

      I’ve been able to successfully perform each step of this process using the API, but it takes several API calls per file. So if the user owns hundreds or thousands of files, in their Drive, the function will likely exceed the 5-minute script execution limit.

      Is there a way to combine or remove any of these steps and still accomplish the same goal?

      Here is the process I have come up with:

      1. Authenticate as User A

      2. Using Files: list, list all Drive files for User A which are owned by them and are not in the trash

      3. Loop through each page of results and store the file details (id, title, parents, etc) in an array

      4. Using Files: insert, create a root-level "container" folder which will eventually contain all of User A’s files and folders, for organization purposes (called "From User A (date)"

      5. Loop through each file in the file details array. If the parent of a file is the root of their Drive, set the parent to be the container folder (Using Files: patch). This moves all of the files they own into the container folder, but keeps the directory structure of subfolders/files intact.

      6. Using Permissions: insert, set User B as an owner of the container folder. This makes the container folder appear in the root of User B’s Drive. However, this does not cascade the Owner permission down to all of the files/folders contained within the container folder.

      7. Using Permissions: insert, set User C as an editor of the container folder so that the folder shows up in their "Shared with me" folder.

      8. Loop through each file in the file details array again and:

        1. Using Permissions: insert, set User B to be an owner of each file

        2. Using Permissions: insert, set User C to be an editor (using the sendNotificationEmails=false parameter so User C doesn’t get flooded with thousands of notification emails)

      9. Authenticate as User B, loop through each file in the file details array again and:

        1. Step 8-1 causes every single file to show up in the root of User B’s Drive as well (when you click on each file, it shows two parent folders: both the container folder AND the My Drive root folder), so now remove the root folder from the parents array of each file (using Files: patch)

        2. Using Permissions: delete, remove User A’s permission from the file (because their account will be set as a "Vault Former Employee," which causes them to still show up in Google Drive Sharing panes, but they shouldn’t because they are no longer with the company).

      解决方案

      Google just released a Data Transfer API that accomplishes most of this. It doesn't handle the parts that relate to User C, but it cuts out most of the steps that took the longest to run.

      这篇关于使用Google Drive API转移所有用户文件所有权的最有效流程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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