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

苏巴卡·慕克吉

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

苏巴卡·慕克吉

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

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何使用 Google Drive API V3 将文件上传到 Group Drive(共享驱动器)?

如何使用Google Drive Java API v3在Google Drive上复制文件?

如何使用python google drive api将文件夹上传到共享的google drive?

如何使用 Python 和 Drive API v3 从 Google Drive 下载文件

如何使用Google Drive API v3上传到Python中的共享驱动器?

如何使用 Google Drive API v3 将可恢复上传到 Python 中的共享文件夹?

如何使用Java中的Google Drive API v3获取上载文件到Google Drive的可共享链接

如何从 Google Drive 上传和下载文件(使用 Rest Api v3)

如何使用NodeJS和Google Drive API v3将文件移至垃圾箱

使用Google Drive API v3移动文件

将所有文件从本地目录上传到Google Drive(Google Drive API V3)

在 Google Drive API V3 中使用 update() 和 addParents 移动文件不会永久生效

如何使用Google Drive Api v3和php显示文本文件的内容?

如何使用Python和Drive API v3下载Google云端硬盘文件

如何使用Google Drive API和Python将文件上传到特定文件夹?

如何使用带有Google Drive API v3 Java的服务帐户访问Team Drive

使用Drive API将图片上传到Google Drive

如何使用Google Drive API将文件上传到Google Drive?

Google Drive Java API V2与V3

使用Google Drive API v3将PDF文件转换为Google文档

Google Drive Api v3和proguard

使用Google Drive API v3获取Google文档的文件内容

获取/生成Google Drive API v3中上传文件的共享链接

如何从 google drive api v3 获取文件列表?

Google Drive PHP v3 API文件元数据

搜索文件 Google Drive API v3

使用Google Drive API在指定的Google Drive中上传文件

使用 Google Apps 脚本将文件上传到 Google Drive

如何在分页和使用排序依据时列出Google Drive API v3上的所有根文件夹和共享?