How to update already existing file in google drive api v3 java

前端 未结 4 697
太阳男子
太阳男子 2021-02-10 19:35

I have tried with the following code... Without any luck...

private void updateFile(Drive service, String fileId) {
        try {
            File file = new Fil         


        
相关标签:
4条回答
  • 2021-02-10 19:57

    To update files content, you can use Files:update, this method supports an /upload URI and accepts uploaded media with the following characteristics:

    • Maximum file size: 5120GB
    • Accepted Media MIME types: /

    This method provides media upload functionality through two separate URIs. For more details, see the document on media upload.

    • Upload URI, for media upload requests:

    PUT https://www.googleapis.com/upload/drive/v2/files/fileId * Metadata URI, for metadata-only requests:

    PUT https://www.googleapis.com/drive/v2/files/fileId

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

    You may also check this SO ticket, the ticket discuss the said error.

    0 讨论(0)
  • 2021-02-10 20:02

    For API v3 the solution proposed by Android Enthusiast does not work unfortunately. The issue is with this bit:

     // First retrieve the file from the API.
    File file = service.files().get(fileId).execute();
    

    doing this it will create a File object, with it's ID field being set, when executing the update, itt will throw an exception, since the ID meta field is not editable directly.

    What you can do is simply create a new file:

    File file = new File();
    

    alter the meta you'd like and update file content if required as shown in the example.

    then simply update the file as proposed above:

    // Send the request to the API.
    File updatedFile = service.files().update(fileId, file, mediaContent).execute();
    

    So based a full example would look like this based on Android Enthusiast solution:

    private static File updateFile(Drive service, String fileId, String newTitle,
    String newDescription, String newMimeType, String newFilename, boolean newRevision) {
    try {
    // First create a new File.
    File file = new File();
    
    // 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;
    }
    }
    
    0 讨论(0)
  • 2021-02-10 20:05

    Source

    Unlike the other answers, this solution allows you to upload without having to create a file for the data on the device first.

     // Create a File containing any metadata changes.
     File metadata = new File().setName(name);
    
     // Convert content to an AbstractInputStreamContent instance.
     ByteArrayContent contentStream = ByteArrayContent.fromString("text/plain", content);
    
    // Update the metadata and contents.
    mDriveService.files().update(fileId, metadata, contentStream).execute();
    
    0 讨论(0)
  • 2021-02-10 20:14

    I can share javascript code for uploading to an already existing file using v3

        const url = 'https://www.googleapis.com/upload/drive/v3/files/' + fileId + '?uploadType=media';
    if(self.fetch){
    var setHeaders = new Headers();
    setHeaders.append('Authorization', 'Bearer ' + authToken.access_token);
    setHeaders.append('Content-Type', mime);
    
    var setOptions = {
        method: 'PATCH',
        headers: setHeaders,
        body:  data 
    };
    fetch(url,setOptions)
        .then(response => { if(response.ok){
        console.log("save to drive");
        }
                else{
                    console.log("Response wast not ok");
                }
                  })
        .catch(error => {
        console.log("There is an error " + error.message);
        });
    
    0 讨论(0)
提交回复
热议问题