操作Drive API 的CRUD
需要以下兩個scope
https:/ |
https:/ |
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();