[精讚] [會員登入]
390

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

你可能感興趣的文章

[web]空氣品質嵌入頁面語法 空氣品質, aqi, ajax

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

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

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

在docker裡跑spring boot+mongo(一) 系統安裝docker ce centos7 + docker ce https://docs.docker.com/in

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

我有話要說

>>

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

訪客留言

[無留言]

隨機好文

java-身份証字號驗証 FormatCheck.java public class FormatCheck { private volatile

[vue.js] 設定 content type 今天在wickt 端怎麼就是收不到vue.js 以post 傳過來的資料 找了好久才發現 application/jso

vue.js component 在parent與child 傳值 component 在parent與child 傳值

python 的RE python re

臺中市雲端校務系統與Windows AD帳號整合(9) 3-2 Linux上隨系統開機啟動服務 以Centos 7 發行版為例 請注意, 此時提供服務的主机為Linux, 所以