[精讚] [會員登入]
188

管理Google共用雲端硬碟

管理Google共用雲端硬碟

分享此文連結 //n.sfs.tw/15992

分享連結 管理Google共用雲端硬碟@igogo
(文章歡迎轉載,務必尊重版權註明連結來源)
2022-12-05 14:03:43 最後編修
2022-12-01 13:01:45 By igogo
 

 

操作Drive API 的CRUD

需要以下兩個scope

 

https://www.googleapis.com/auth/drive
https://www.googleapis.com/auth/drive.file

 

https://developers.google.com/drive/api/v3/reference/permissions/update

 

一.共用雲端硬碟

重點在這句, 需使用useDomainAdminAccess參數

Use the useDomainAdminAccess parameter with the drives and permissions resources to manage shared drives across an organization.

 

 

//https://developers.google.com/drive/api/guides/manage-shareddrives

 DriveList driveList = service.drives().list()
          .setQ("organizerCount = 0")
          .setFields("nextPageToken, drives(id, name)")
          .setUseDomainAdminAccess(true)
          .setPageToken(pageToken)
          .execute();

 

setQ 為篩選資料夾條件, 例如

.setQ("memberCount = 0")  //雲端硬碟沒有成員

.setQ("organizerCount = 0")  //沒有管理者

 

.setFields("nextPageToken, drives(id, name)")

回傳id,及name值

 

二 Permission

每個雲端硬碟會包含多個permission

https://developers.google.com/drive/api/v3/reference/permissions

 

列出特定共用雲端硬碟的所有permission

操作時需要加上

.setUseDomainAdminAccess(true)
.setSupportsAllDrives(true)

 

否則只會找my drive, 發生找不到訊息

 

DriveList driveList = service.drives().list()
                    .setQ("name contains 'VMs'")
                    .setFields("nextPageToken, drives(id, name)")
                    .setUseDomainAdminAccess(true)
                    .setPageToken(pageToken)
                    .execute();

            driveList.getDrives().forEach(drive -> {

                logger.info(drive.getName());

//                列出permission list
                try {
                    PermissionList permissionList = service.permissions()
                            .list(drive.getId())
                            .setFields("nextPageToken, permissions(id,emailAddress,role)")
                            .setUseDomainAdminAccess(true)
                            .setSupportsAllDrives(true)
                            .execute();

                    permissionList.getPermissions().forEach(permission -> {
                        logger.info(permission.getEmailAddress());
                    });

                } catch (IOException e) {
                    e.printStackTrace();
                }

            });

pageToken = driveList.getNextPageToken();

}

.setFields("nextPageToken, permissions(id,emailAddress,role)")

可以回傳id,emailAddress,及role

 

將所有的成員都設定為檢視者

try {
                    PermissionList permissionList = service.permissions()
                            .list(drive.getId())
                            .setFields("nextPageToken, permissions(id,emailAddress)")
                            .setUseDomainAdminAccess(true)
                            .setSupportsAllDrives(true)
                            .execute();

                    permissionList.getPermissions().forEach(permission -> {
                        logger.info("設定唯讀:" + permission.getEmailAddress());

                        try {
                            Permission readPermission = new Permission();
                            readPermission.setRole("reader");

                            service.permissions()
                                    .update(drive.getId(), permission.getId(), readPermission)
                                    .setUseDomainAdminAccess(true)
                                    .setSupportsAllDrives(true)
                                    .execute();
                            Thread.sleep(35); //避免密集request google api
                        } catch (Exception e) {
                            logger.info(e.getMessage());
                        }
                    });

                } catch (IOException e) {
                    e.printStackTrace();
                }

 

如果成員已是reader, 送request 進去會出現底下誤誤訊息,  查了google似乎是bug

{
  "code": 403,
  "errors": [
    {
      "domain": "global",
      "message": "The authenticated user does not have the required access to update the permission.",
      "reason": "cannotUpdatePermission"
    }
  ],
  "message": "The authenticated user does not have the required access to update the permission."
}

 

所以就catch exception,  但是不管它, 繼續執行即可

 

移除permission

 

    PermissionList permissionList = service.permissions()
                                .list(drive.getId())
                                .setFields("nextPageToken, permissions(id,emailAddress,role)")
                                .setUseDomainAdminAccess(true)
                                .setSupportsAllDrives(true)
                                .execute();

                        permissionList.getPermissions().forEach(permission -> {
                            if (permission.getEmailAddress().matches("igogo@st.tc.edu.tw")) {
                                try {
                                    service.permissions()
                                            .delete(drive.getId(),permission.getId())
                                            .setUseDomainAdminAccess(true)
                                            .setSupportsAllDrives(true)
                                            .execute();
                                } catch (IOException e) {
                                    throw new RuntimeException(e);
                                }
                            }
                        });

 

 

刪除檔案

無法直接刪除drive, 如果有檔案會出現"This resource cannot be deleted because it has children“

所以要先刪光資料夾底下的檔案

 

  String fileNextPageToken = "";

                while (fileNextPageToken != null) {
                    try {
                        FileList fileList = service.files().list()
                                .setDriveId(drive.getId())
                                .setPageSize(1000)
                                .setIncludeItemsFromAllDrives(true)
                                .setSupportsAllDrives(true)
                                .setCorpora("drive")
                                .execute();

                        logger.info(drive.getName() + "," + fileList.getFiles().size());
                        if (fileList.getFiles().size() == 0) {
                            //刪資料夾
                            logger.info("delete drive: " + drive.getName());
                            deleteDrive(drive.getId());
                            Thread.sleep(35);
                        } else {
                            fileList.getFiles().forEach(file -> {
                                try {
                                    logger.info("delete file: " + drive.getName() + "," + file.getName());
                                    service.files()
                                            .delete(file.getId())
                                            .setSupportsAllDrives(true)
                                            .execute();
                                    Thread.sleep(35);
                                } catch (Exception e) {
                                    logger.info(e.getMessage());
                                }
                            });
                        }
                        fileNextPageToken = fileList.getNextPageToken();

                    } catch (Exception e) {
                        logger.info(e.getMessage());
                    }


                }

            });
            pageToken = driveList.getNextPageToken();

 

END

你可能感興趣的文章

對照mac address 批次更改電腦名稱 讀取mac address 對照表, 還原電腦後,自動更改密碼 'show logged on user Dim

雲端校務系統與OPENLDAP帳號整合(2) openldap,雲端校務

如何檢查網站憑證是否過期 檢查網站憑證

vim 特定範圍行數開頭加上# 註解 vim 特定範圍行數開頭加上# 註解

臺中市雲端校務系統與Windows AD帳號整合(7) 設定校端更改密碼程式及執行

[shell script] 批次判斷domain name 正解設定 判斷dns 正解設定

我有話要說

>>

限制:留言最高字數1000字。 限制:未登入訪客,每則留言間隔需超過10分鐘,每日最多5則留言。

訪客留言

[無留言]

隨機好文

tmux 將終端機切割成多個視 窗 tmux, multiple terminal

apache 反向代理 80轉443轉8080 apache, proxy pass

proxmox lxc 救援 今天突然接到一名強者我朋友的臨時求援 他說他把pve 從5.1升到5.2後, kvm的虛擬机器都沒問題 , 但是lxc的

2018 hoc 掃地機器人 掃地機器人只能打掃沒有障礙物(桌椅、牆壁)的範圍,請寫程式控制機器人打掃餐廳的所有走道, 並在清掃完畢後回到充電器。

ArrayList 想移除特定值 想移出water, 使用lambda 的方式如下 List<String> fruits = new Arr