检索我的域的Google云端硬盘中的文档数量(在Java中)

时间:2015-01-20 15:20:56

标签: java google-api google-drive-api google-docs

我是拥有超过100,000名用户的Google域管理员。 我想知道我的域中有多少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");
    }

2 个答案:

答案 0 :(得分:0)

我相信您可以使用Google云端硬盘Web API自定义请求,并按照建议使用file.listchildren.list。您可以指定root在您的域的根目录下搜索,或指定自定义查询以获取由/可写的文件列表等。

请查看[Google Drive Web API] [1]

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

答案 1 :(得分:0)

最后,我得到了调用Admin SDK API的customerUsageReports()方法并传递&#34; docs:num_docs&#34;作为参数。这是我的代码:

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();
}