[精讚] [會員登入]
421

管理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

你可能感興趣的文章

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

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

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

網站無障礙規範 https://accessibility.ncc.gov.tw/News/Detail/3238?Category=4

proxmox zfs 建立異地備份與還原 zfs最吸引人的地方就是可以建立快照並傳輸到遠端机器, 達到增量備份, 並且隨時從任一備份檔還原 完全不怕勒索病毒 將v

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

我有話要說

>>

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

訪客留言

[無留言]

隨機好文

臺中市校務雲端系統與Windows AD帳號整合(1) active directory,ldaps,雲端校務系統

臺中市雲端校務系統與Windows AD帳號整合(5) 安裝java環境

台中市網路應用競賽試場安排(一) 排試場就跟排班或是排課表一樣, 找不到一個都能讓所有人滿意的結果

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

如何在docker 的nginx 做另一個container的反向代理 環境 docker 中一container 跑 nginx 對外服務80 port, http://server 另一c