我只想更新现有Google驱动器文件的元数据(例如描述或文件名),
使用javascript Google Drive Update Documentation可以做到这一点.但是没有Java文档.
我也找到了执行此操作的代码
private static File updateFile(Drive service,String fileId,String newTitle,String newDescription,String newMimeType,String newFilename,boolean newRevision) {
try {
// First retrieve the file from the API.
File file = service.files().get(fileId).execute();
// File's new Metadata.
file.setTitle(newTitle);
file.setDescription(newDescription);
file.setMimeType(newMimeType);
// File's new content.
java.io.File fileContent = new java.io.File(newFilename);
FileContent mediaContent = new FileContent(newMimeType,fileContent);
// Send the request to the API.
File updatedFile = service.files().update(fileId,file,mediaContent).execute();
return updatedFile;
} catch (IOException e) {
System.out.println("An error occurred: " + e);
return null;
}
}
// ...
}
但是在此它也可以更新内容(我不想这么做).
我怎样才能做到这一点?
最佳答案
根据更新元数据的文档,您有单独的URL,
原文链接:https://www.f2er.com/android/531371.htmlPUT https://www.googleapis.com/upload/drive/v2/files/fileId
元数据URI,仅用于元数据的请求:
PUT https://www.googleapis.com/drive/v2/files/fileId
更多关于,
https://developers.google.com/drive/api/v2/reference/files/update#try-it
与Java无关.击中正确的URL好友.
他们有Java示例来更新元数据(仅),
https://developers.google.com/drive/api/v2/reference/files/patch
代码:
import com.google.api.services.drive.Drive;
import com.google.api.services.drive.Drive.Files;
import com.google.api.services.drive.model.File;
import java.io.IOException;
// ...
public class MyClass {
// ...
/**
* Rename a file.
*
* @param service Drive API service instance.
* @param fileId ID of the file to rename.
* @param newTitle New title for the file.
* @return Updated file Metadata if successful,{@code null} otherwise.
*/
private static File renameFile(Drive service,String newTitle) {
try {
File file = new File();
file.setTitle(newTitle);
// Rename the file.
Files.Patch patchRequest = service.files().patch(fileId,file);
patchRequest.setFields("title");
File updatedFile = patchRequest.execute();
return updatedFile;
} catch (IOException e) {
System.out.println("An error occurred: " + e);
return null;
}
}
// ...
}