我需要使用 Java 将文件上传到共享的 Google Drive 位置(该位置不属于我,而是与我共享)。使用Drive API s,我们可以将文件上传到用户拥有的驱动器位置,但尚未找到任何允许上传到共享位置的解决方案。用例类似于应用程序的不同用户需要将文件上传到共享的 Google 云端硬盘位置。关于这个主题还有几个其他问题(即 this ),但没有一个有正确的答案。如果可能,请提供帮助,否则请告知无法以编程方式实现此目的。
最佳答案
我在评论者@MateoRandwolf 的帮助下找到了解决方案,因此发布了答案。希望它有帮助..
据此documentation ,supportsAllDrives=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/