使用Google Drive API进行递归搜索 [英] Recursive search with Google Drive API
问题描述
我正在使用Google Drive API v2。
我试图使用API中包含的搜索功能实现搜索功能。
似乎children.list不是递归的,这意味着它不会查看所有的子文件夹。
对于每个文件夹子引用,我是否必须通过多次调用API来实现它?或者我在这里丢失了什么?
感谢您的帮助。 当你得到一个ChildReference的列表,你不能确定它们是否是一个文件夹,除非你真的检索文件。所以如果你正在做一个大的递归搜索,你应该使用批处理来实现这一点。我创建了一个递归地更改文件夹中所有文件的所有者的方法,这可能有帮助:
private void updateFolderOwner (String folderId,final String newOwnerEmail)throws IOException {
String permissionId = service.permissions()。getIdForEmail(newOwnerEmail).execute()。getId();
Children.List request = service.children()。list(folderId);
JsonBatchCallback< Permission> callback = new JsonBatchCallback< Permission>(){
@Override
public void onSuccess(Permission permission,HttpHeaders responseHeaders){}
$ b @Override
public void onFailure GoogleJsonError e,HttpHeaders responseHeaders){
System.out.println(Error Message:+ e.getMessage());
}
};
JsonBatchCallback< File> findFolderCallback = new JsonBatchCallback< File>(){
@Override $ b $ public void onSuccess(File file,HttpHeaders responseHeaders)throws IOException {
if(file!= null){
String fileExtension = file.getFileExtension();
String mimeType = file.getMimeType();
if(mimeType!= null&& mimeType.equals(application / vnd.google-apps.folder)&&(fileExtension == null || fileExtension.equals())) {
//递归搜索此文件夹...
updateFolderOwner(file.getId(),newOwnerEmail);
$ b @Override
public void onFailure(GoogleJsonError e,HttpHeaders responseHeaders){
System.out.println(错误信息: + e.getMessage());
}
};
do {
try {
ChildList children = request.execute();
BatchRequest changeOwnerBatch = service.batch();
BatchRequest findFolderBatch = service.batch();
权限许可= new Permission();
permission.setEmailAddress(newOwnerEmail);
permission.setValue(newOwnerEmail);
permission.setType(user);
permission.setRole(owner); (ChildReference child:children.getItems()){
service.files()。get(child.getId())。queue(findFolderBatch,findFolderCallback);
。
service.permissions()。update(child.getId(),permissionId,permission).setTransferOwnership(true).queue(changeOwnerBatch,callback);
}
changeOwnerBatch.execute();
findFolderBatch.execute();
request.setPageToken(children.getNextPageToken());
catch(IOException e){
System.out.println(发生错误:+ e);
request.setPageToken(null); (request.getPageToken()!= null&&&& amp; request.getPageToken()。length()> 0);
}
}
}
I'm working with the Google Drive API v2.
I trying to implement a search feature using the one included in the API.
It seems that the "children.list" is not recursive, meaning it doesn't look through all the subfolders. Do I have to implement it via multiple calls to the API, for each folder child reference? Or am I missing something here?
Thanks for your help.
When you get a list of ChildReferences, you cannot determine if they are a folder or not unless you actually retrieve the file. So if you are doing a large recursive search you should use batching to achieve this. I created a method to recursively change the owner of all the files in a folder, this might be of help:
private void updateFolderOwner(String folderId, final String newOwnerEmail) throws IOException{
String permissionId = service.permissions().getIdForEmail(newOwnerEmail).execute().getId();
Children.List request = service.children().list(folderId);
JsonBatchCallback<Permission> callback = new JsonBatchCallback<Permission>() {
@Override
public void onSuccess(Permission permission, HttpHeaders responseHeaders) {}
@Override
public void onFailure(GoogleJsonError e, HttpHeaders responseHeaders) {
System.out.println("Error Message: " + e.getMessage());
}
};
JsonBatchCallback<File> findFolderCallback = new JsonBatchCallback<File>() {
@Override
public void onSuccess(File file, HttpHeaders responseHeaders) throws IOException {
if (file != null) {
String fileExtension = file.getFileExtension();
String mimeType = file.getMimeType();
if (mimeType != null && mimeType.equals("application/vnd.google-apps.folder") && (fileExtension == null || fileExtension.equals(""))) {
//Recursively search this folder...
updateFolderOwner(file.getId(),newOwnerEmail);
}
}
}
@Override
public void onFailure(GoogleJsonError e, HttpHeaders responseHeaders) {
System.out.println("Error Message: " + e.getMessage());
}
};
do {
try {
ChildList children = request.execute();
BatchRequest changeOwnerBatch = service.batch();
BatchRequest findFolderBatch = service.batch();
Permission permission = new Permission();
permission.setEmailAddress(newOwnerEmail);
permission.setValue(newOwnerEmail);
permission.setType("user");
permission.setRole("owner");
for (ChildReference child : children.getItems()) {
service.files().get(child.getId()).queue(findFolderBatch, findFolderCallback);
service.permissions().update(child.getId(), permissionId, permission).setTransferOwnership(true).queue(changeOwnerBatch, callback);
}
changeOwnerBatch.execute();
findFolderBatch.execute();
request.setPageToken(children.getNextPageToken());
} catch (IOException e) {
System.out.println("An error occurred: " + e);
request.setPageToken(null);
}
} while (request.getPageToken() != null && request.getPageToken().length() > 0);
}
这篇关于使用Google Drive API进行递归搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!