java - 使用 Java 和 Google Drive API V3 将文件上传到共享的 Google 云端硬盘位置?

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

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

最佳答案

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

据此documentationsupportsAllDrives=true 参数通知 Google 云端硬盘您的应用程序旨在处理共享驱动器上的文件。但也提到,supportsAllDrives 参数的有效期到 2020 年 6 月 1 日。2020 年 6 月 1 日之后,所有应用程序将被假定支持共享驱动器。因此我尝试使用Google Drive V3 Java API,发现V3 API的Drive.Files.Create类的execute方法目前默认支持共享驱动器。附上示例代码片段供其他人引用。此方法 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();
}

关于java - 使用 Java 和 Google Drive API V3 将文件上传到共享的 Google 云端硬盘位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61128958/

相关文章:

java - 解析 ksoap2 响应

google-api - 调用 "Users.messages: list"时 Gmail API 以什么顺序返回消息

google-api - 如何对 Google Drive API 中的文件触发病毒检查?

google-drive-api - 从服务帐户上传的文件的 Google Drive API 所有权

java - 套接字关闭问题 - 最后一部分数据丢失

java - Hashmap 中一个键的多个值

iOS Swift YouTube 数据 API v3 集成崩溃应用程序

java - 安卓谷歌驱动器 SDK : Saving to App Folder

java - 一起使用 EJB3 和 Struts 2

ios - 如何从 SVN 下载谷歌代码到 Mac