我是一个拥有超过100.000用户的谷歌域的管理员。我想知道我的域中有多少个Google文档,所以我的方法是检索所有用户并迭代它们调用驱动器API,并为每个文档增加一个计数器,但这需要很多时间。有没有其他方法可以做到这一点?这是我的代码:
String emailAddress = "xxxxxxxxxxxxxxxxxxxxx@developer.gserviceaccount.com";
JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
HttpTransport httpTransport;
Long number_docs = (long) 0;
try {
httpTransport = GoogleNetHttpTransport.newTrustedTransport();
List<String> serviceAccountScopes = new ArrayList<String>();
serviceAccountScopes.add("https://www.googleapis.com/auth/drive")
serviceAccountScopes.add("https://www.googleapis.com/auth/admin.directory.group");
serviceAccountScopes.add("https://www.googleapis.com/auth/admin.directory.user");
GoogleCredential credential = new GoogleCredential.Builder()
.setTransport(httpTransport)
.setJsonFactory(JSON_FACTORY)
.setServiceAccountId(emailAddress)
.setServiceAccountUser("user@domain.com")
.setServiceAccountPrivateKeyFromP12File(new File("xxx.p12"))
.setServiceAccountScopes(serviceAccountScopes).build();
Directory directory = new Directory.Builder(httpTransport,JSON_FACTORY, credential).setApplicationName("xxxxxxxxx.apps.googleusercontent.com").build();
Directory.Users.List request = directory.users().list().setCustomer("my_customer").setDomain("domain.com");
do {
Users currentPage = request.execute();
List<User> usuarios = currentPage.getUsers();
for (User u : usuarios) {
GoogleCredential credentialUser = new GoogleCredential.Builder()
.setTransport(httpTransport)
.setJsonFactory(JSON_FACTORY)
.setServiceAccountId(emailAddress)
.setServiceAccountUser(u.getPrimaryEmail())
.setServiceAccountPrivateKeyFromP12File(new File("xxxxxxx.p12"))
.setServiceAccountScopes(serviceAccountScopes).build();
Drive drive = new Drive.Builder(httpTransport,JSON_FACTORY, credentialUser)
.setApplicationName("xxxxxxxxxxxxxxxxx.apps.googleusercontent.com")
.build();
About about = drive.about().get().execute();
Files.List files = drive.files().list();
Long docs_cont = (long) 0;
used_quota = used_quota + about.getQuotaBytesUsed();
do{
FileList file_list = files.execute();
List<com.google.api.services.drive.model.File> files_list = file_list.getItems();
docs_cont = docs_cont + files_list.size();
request.setPageToken(file_list.getNextPageToken());
} while (files.getPageToken() != null && files.getPageToken().length() > 0);
number_docs = number_docs + docs_cont;
}
} while (request.getPageToken() != null && request.getPageToken().length() > 0);
}catch(Exception e){
//TODO:
}finally{
System.out.println("there are " + number_docs + " docs in the domain");
}
我相信您可以使用Google Drive Web API来自定义请求,并按照建议使用file.list
或children.list
。您可以指定root
在域根目录下搜索,也可以指定自定义查询以获取由等拥有/可写的文件列表。
请看一下[Google Drive Web API][1]
[1]:https://developers.google.com/drive/web/search-parameters。希望这有帮助。
最后,我得到了调用管理员 SDK API 的 customerUsageReports() 方法的解决方案,并将"docs:num_docs"作为参数传递。 这是我的代码:
String emailAddress = "xxxxxxxxxxxxxxxxxxxxxxx@developer.gserviceaccount.com";
JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
HttpTransport httpTransport;
try {
httpTransport = GoogleNetHttpTransport.newTrustedTransport();
List<String> serviceAccountScopes = new ArrayList<String>();
serviceAccountScopes.add("https://www.googleapis.com/auth/admin.reports.usage.readonly");
serviceAccountScopes.add("https://www.googleapis.com/auth/admin.reports.audit.readonly");
GoogleCredential credential = new GoogleCredential.Builder()
.setTransport(httpTransport)
.setJsonFactory(JSON_FACTORY)
.setServiceAccountId(emailAddress)
.setServiceAccountUser("user@domain.com")
.setServiceAccountPrivateKeyFromP12File(new File("xxxxxxxx.p12"))
.setServiceAccountScopes(serviceAccountScopes).build();
// Admin SDK API call - Directory
Reports report_builder = new Reports.Builder(httpTransport, JSON_FACTORY, credential)
.setApplicationName("xxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com")
.build();
UsageReports rep = report_builder.customerUsageReports().get("2015-01-01")
.setParameters("docs:num_docs").execute();
UsageReport report = rep.getUsageReports().get(0);
Long num_docs = (long) = report.getParameters().get(0).getIntValue();
System.out.println("There are " + num_docs.toString() + " docs in the domain");
}catch(Exception e){
//TODO:
e.printStackTrace();
}