gpt4 book ai didi

java - 使用 Java 仅更新现有 Google Drive 文件的元数据

转载 作者:太空狗 更新时间:2023-10-29 13:01:59 25 4
gpt4 key购买 nike

我只想更新现有 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,

上传 URI,用于媒体上传请求:

PUT 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 fileId, 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;
}
}

// ...
}

关于java - 使用 Java 仅更新现有 Google Drive 文件的元数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55491213/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com