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

前端 未结 4 714
太阳男子
太阳男子 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.

自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题