仅更新现有 Google 驱动器文件的元数据使用 Java

Updating only metadata of existing Google Drive file Using Java

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

  // ...
}

经过更多研究,我找到了解决方案,

   File file =new File();
     file.setName("new name");
     service.files().update(fileid, file).execute();

此代码将更新文件名。