【问题标题】:Upload a file to shared google drive location using Java with Google Drive API V3?使用 Java 和 Google Drive API V3 将文件上传到共享的 Google Drive 位置?
【发布时间】:2020-07-22 12:48:14
【问题描述】:

我需要使用 Java 将文件上传到共享的 Google Drive 位置(不属于我,而是与我共享)。使用Drive APIs,我们可以将文件上传到用户拥有的驱动器位置,但还没有找到任何解决方案来允许上传到共享位置。用例类似于应用程序的不同用户需要将文件上传到共享的 Google Drive 位置。关于此主题的其他问题很少(即this),但没有一个有正确的答案。如果可能,请提供帮助,或者请告知无法以编程方式实现此目标。

【问题讨论】:

  • 根据the documentation,您需要添加查询参数supportsAllDrives=true才能从Drive API访问、上传、列出或创建共享驱动器。也许this Stack Overflow solution 可以更好地阐明这一点。 让我知道这是否有效或您是否需要更多信息。
  • @MateoRandwolf 我会告诉你的。非常感谢
  • @MateoRandwolf,它起作用了,实际上按照这个documentation,他们将弃用这个参数,可能是因为这个原因,他们在他们的java apis中默认支持它。所以实际上,我什么都不用做。 :)

标签: java google-api google-drive-api google-api-java-client google-drive-android-api


【解决方案1】:

我在评论者@MateoRandwolf 的帮助下找到了解决方案,因此发布了答案。希望对你有帮助。。

根据documentationsupportsAllDrives=true 参数通知 Google Drive,您的应用程序旨在处理共享驱动器上的文件。但也提到supportsAllDrives参数有效期至2020年6月1日。2020年6月1日之后,假设所有应用程序都支持共享驱动器。因此,我尝试使用 Google Drive V3 Java API,发现当前在 V3 API 的 Drive.Files.Create 类的 execute 方法中默认支持共享驱动器。附上示例代码 sn-p 供其他人参考。这个方法uploadFile使用直接上传的方式将文件上传到谷歌驱动文件夹,并返回上传的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();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-10
    • 2016-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多