I get the following Java code from https://developers.google.com/drive/v2/reference/files/update

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;
    }
  }

  // ...
}

I would like to set newRevision to false. However, the code example doesn't show how we can apply it?

Any idea how we can apply newRevision?

有帮助吗?

解决方案

There is solution for c# for Drive.Files.Update.

To set the newRevision you should first get the request, set the flag, then execute.

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

// set newRevision
request.setNewRevision(newRevision);

// execute
File updatedFile = request.execute();
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top