简体   繁体   English

使用 Java 和 Google Drive API V3 将文件上传到共享的 Google Drive 位置?

[英]Upload a file to shared google drive location using Java with Google Drive API V3?

I need to upload files to a shared Google Drive location(which is not owned by me, rather shared with me) using Java.我需要使用 Java 将文件上传到共享的 Google Drive 位置(不属于我,而是与我共享)。 Using the Drive API s, We can upload files to a drive location which the user owns, But have not found any solution to allow upload to a shared location.使用驱动器 API s,我们可以将文件上传到用户拥有的驱动器位置,但尚未找到任何允许上传到共享位置的解决方案。 The use case is something like different users of an application need to upload files to a shared Google Drive location.用例类似于应用程序的不同用户需要将文件上传到共享的 Google Drive 位置。 There are few other questions( ie this ) asked on this topic, but none of them has proper answer.关于此主题的其他问题(即this )很少,但没有一个有正确的答案。 Please help if it is possible or please inform it is not possible to achieve this programmatically.如果可能,请提供帮助,或者请告知无法以编程方式实现此目的。

I found the solution with the help of fellow commenter @MateoRandwolf, Hence posting the answer.我在评论者@MateoRandwolf 的帮助下找到了解决方案,因此发布了答案。 Hope it helps..希望能帮助到你..

As per this documentation , the supportsAllDrives=true parameter informs Google Drive that your application is designed to handle files on shared drives.根据本 文档supportsAllDrives=true参数通知 Google Drive 您的应用程序旨在处理共享驱动器上的文件。 But it is also mentioned that the supportsAllDrives parameter will be valid until June 1, 2020. After June 1, 2020, all applications will be assumed to support shared drives.但也提到, supportsAllDrives参数有效期至 2020 年 6 月 1 日。2020 年 6 月 1 日之后,将假定所有应用程序都支持共享驱动器。 So I tried with the Google Drive V3 Java API, and found that shared drives are currently supported by default in the execute method of Drive.Files.Create class of V3 APIs.于是我用Google Drive V3 Java API试了一下,发现V3 API的Drive.Files.Create class的execute方法目前默认支持共享驱动器。 Attaching a sample code snippet for others reference.附上示例代码片段供其他人参考。 This method uploadFile uploads a file to Google drive folder using direct upload and returns the uploaded fileId.此方法uploadFile使用直接上传将文件上传到 Google 驱动器文件夹并返回上传的 fileId。

public static String uploadFile(Drive drive, String folderId) throws IOException {

    /*
    * drive: an instance of com.google.api.services.drive.Drive class
    * folderId: The id of the folder where you want to upload the file, It can be
    * located in 'My Drive' section or 'Shared with me' shared drive with proper 
    * permissions.
    * */

    File fileMetadata = new File();
    fileMetadata.setName("photo.jpg");
    fileMetadata.setParents(Collections.singletonList(folderId));
    java.io.File filePath = new java.io.File("files/photo.jpg");
    FileContent mediaContent = new FileContent("image/jpeg", filePath);

    File file = drive.files().create(fileMetadata, mediaContent)
                                    .setFields("id")
                                    .execute();
    System.out.println("File ID: " + file.getId());
    return file.getId();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM