检索我域的 Google 驱动器中的文档数(在 Java 中)

Retrieve the number of docs in Google Drive of my domain (In Java)

我是 Google 域的管理员,该域拥有超过 100.000 名用户。 我想知道我的域中有多少 Google 文档,所以我的方法是检索所有用户并迭代他们调用 drive 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.listchildren.list。您可以指定 root 在您域的根目录下搜索或指定自定义查询以获取由等拥有/可写的文件列表

请看一下[Google Drive Web APIs][1]

[1]: https://developers.google.com/drive/web/search-parameters。希望这有帮助。

最后我得到了调用 Admin 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();
}