上传文件时如何在Google Drive API中使用newRevision参数

时间:2014-05-02 16:30:41

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

我从https://developers.google.com/drive/v2/reference/files/update

获得以下Java代码
import com.google.api.client.http.FileContent;
import com.google.api.services.drive.Drive;
import com.google.api.services.drive.model.File;

import java.io.IOException;
// ...

public class MyClass {

  // ...

  /**
   * Update an existing file's metadata and content.
   *
   * @param service Drive API service instance.
   * @param fileId ID of the file to update.
   * @param newTitle New title for the file.
   * @param newDescription New description for the file.
   * @param newMimeType New MIME type for the file.
   * @param newFilename Filename of the new content to upload.
   * @param newRevision Whether or not to create a new revision for this
   *        file.
   * @return Updated file metadata if successful, {@code null} otherwise.
   */
  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;
    }
  }

  // ...
}

我想将newRevision设为false。但是,代码示例没有显示我们如何应用它?

知道我们如何申请newRevision

1 个答案:

答案 0 :(得分:1)

c#Drive.Files.Update的解决方案。

要设置newRevision,首先应该获取请求,设置标志,然后执行。

Drive.Files.Update request = service.files().update(fileId, file, mediaContent);

// set newRevision
request.setNewRevision(newRevision);

// execute
File updatedFile = request.execute();