微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用Java仅更新现有Google云端硬盘文件的元数据

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

  // ...
}

原文地址:https://www.jb51.cc/android/531371.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐