[精讚] [會員登入]
241

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

你可能感興趣的文章

批次檢查tls憑證是否過期 tls憑證一年一簽, 台中市有三百多所學校, 寫成一檔案 skaps.tc.edu.tw www.skaps.tc.ed

ubuntu ufw ufw 簡易筆記 原則禁止,例外開放 ufw default deny 啟動ufw sudo ufw enable 關掉

使用expect及 openssl 產生SSL 憑證簽署要求 每年都要製做tls 憑證 二十幾張, 此語法利用expect 產生私錀及憑證請求檔 #!/usr/bin/expect

使用vbs設定windows 10 靜態IP setting static ip Set Adapter = GetObject("winmgmts:Win

openldap資料移到 docker 拉docker 上的 image 回來 docker pull osixia/openldap 在正在運行的openld

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

我有話要說

>>

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

訪客留言

[無留言]

隨機好文

[vue.js] input event Form-Input-Components-using-Custom-Events

centos 7 移机出現 dracut-initqueue timeout centos 7 移机出現 dracut-initqueue timeout 處理

00-F2 的 IPV6 反解設定 近日, 強者我的大神同事, line 傳來一句: igogo 上次你那個ipv6的設定檔 再幫我跑一次 如連結 程式都寫

找尋多個文件夾中最新檔案(jpg,png,txt)並複製到nexus資料夾 找尋多個文件夾中最新檔案(jpg,png,txt)並複製到集中資料夾

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