0

how can I replace the content of the file referenced by fileId with a new content in java.io.File newFileContent. The following function update the content of the file with an empty content

public static void updateDriveFile(Drive service, java.io.File newFileContent, String fileId) {

    com.google.api.services.drive.model.File emptyContent = new File();
    emptyContent.setTrashed(true);
    service.files().update(fileId, emptyContent).execute();
}
Yusof
  • 37
  • 2
  • 6

2 Answers2

2

The way to update files is changed with v3.

You should not pass the file that was retrieved from google drive as is, rather than you should pass a new file object with your updates only. Example:

File gDriveFile = getAnyFileFromGdrive(.....);

java.io.File fileContent = new java.io.File("<path-of-new-file-on-your-hard-disk>");
FileContent mediaContent = new FileContent("text/plain", fileContent);

File fileObjectWithUpdates = new File();
fileObjectWithUpdates .setDescription("This file was updated");

File updatedFile = drive.files().update(gDriveFile.getId(), fileObjectWithUpdates, mediaContent).execute();

Note that, except from it's id, we are not using the 'gDriveFile' object for anything else. In order to update:

  • The file content --> use a 'FileContent' object
  • The file description (or any other property) --> use a new 'File' object

You can find more migration guidelines here.

Mario
  • 767
  • 1
  • 14
  • 42
0

You should use a combination of FileContent class and update() method with three arguments as shown in the example below :

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

  // ...
}
Soorena
  • 4,352
  • 5
  • 30
  • 42
  • 1
    This was working on API v2 but doesn't work on API v3, error that I get : com.google.api.client.googleapis.json.GoogleJsonResponseExce‌​ption: 403 Forbidden { "code" : 403, "errors" : [ { "domain" : "global", "message" : "The resource body includes fields which are not directly writable.", "reason" : "fieldNotWritable" } ], "message" : "The resource body includes fields which are not directly writable." } – Yusof Oct 02 '16 at 18:56
  • @Yusof the best way that I found is to delete the previous file and set the new contents again. as shown [here](https://developers.google.com/drive/v3/web/manage-uploads#importing_to_google_docs_types_wzxhzdk8wzxhzdk9) – Soorena Oct 02 '16 at 20:28