【问题标题】:Updating only metadata of existing Google Drive file Using Java使用 Java 仅更新现有 Google Drive 文件的元数据
【发布时间】:2019-04-03 09:05:10
【问题描述】:

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

  // ...
}

但在这方面它也会更新内容(我不想这样做)。 我怎样才能做到这一点?

【问题讨论】:

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


    【解决方案1】:

    根据更新元数据的文档,您有单独的 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代码中,我做了几个谷歌驱动操作,比如创建文件夹、搜索文件夹、下载文件夹、获取文件的属性。对于所有这些,我使用了 google drive api。所以我认为我们可以在不使用 URL 的情况下以这种方式做到这一点。
    • Google Drive update docmentation 如您在本文档中所见,有代码可以一起更新元数据和内容。但不仅适用于元数据!
    • 编辑了我的答案希望它能解决您的问题,如果是这样,请投票并接受我的答案。
    • 你提到这个Google Drive update docmentation 吗?您可以看到他们正在向 API service.files().update(fileId, file, mediaContent).execute(); 发送请求,第三个参数仅是文件内容。如果没有第三个参数,我们不能调用这个 API 请求。
    【解决方案2】:

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

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

    此代码将更新文件名。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-08
      • 1970-01-01
      相关资源
      最近更新 更多