我在评论者@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] 删除。
我来说两句