如何从使用Java的谷歌驱动器获得完整的文件路径 [英] How to get full file path from google drive using java

查看:152
本文介绍了如何从使用Java的谷歌驱动器获得完整的文件路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是java开发人员。我使用google应用程序client_id和client_secrect将谷歌驱动器集成到我的应用程序中。
基于这个client_id,我得到了用户的访问令牌。获得访问令牌后,我使用以下代码从驱动器获取文件和文件夹。

  HttpTransport httpTransport = new NetHttpTransport(); 
JacksonFactory jsonFactory = new JacksonFactory();
System.out.println(inventoryIntegrationConfig.getAccessToken());

GoogleCredential凭证=新的GoogleCredential()。setAccessToken(inventoryIntegrationConfig.getAccessToken());

Drive service = new Drive.Builder(httpTransport,jsonFactory,null)
.setApplicationName(Akoonu)
.setHttpRequestInitializer(凭证).build();

Files.List request = service.files()。list();




我成功接收到文件和文件夹下载网址和文件名。但我无法获取完整文件例如
BaseFolder / SubFolder1 / Subofsubfolder / testfile.txt。
在这里我的预期输出是路径:BaseFolder / SubFolder1 / Subofsubfolder /



我无法从任何Drive类获取完整文件路径。您可以告诉我如何获得完整的文件路径?
我们需要使用哪种方法来获得完整的文件路径?



请在这里帮我。

解决方案

没有方法调用/获取完整的文件路径。首先让我们澄清关于文件完整路径的一些细节。停止考虑文件夹和路径,并考虑像标签(父母和收藏)这样的字词。


父母

包含文件的父文件夹的ID。
如果未将其指定为创建请求的一部分,则该文件将直接放置在我的云端硬盘文件夹中。更新请求必须使用 addParents removeParents 参数修改值。 >一个文件可以有多个父代,每个父代又可以有一个或多个父代/标签/集合。要获得文件路径,我们需要递归获取其父项的父项。根据pinoyyid回答的SO门票,请记住,一个文件可以有多个父母,每个父母也可以有多个父母,因此一个文件可以有多个路径。



使用Google Drive,忽略术语路径/文件夹,而仅仅是文件的一个属性。



以下是 Apps脚本,它创建了一个获取文件的父母/集合和相关 SO问题 a>获取更多信息。我希望它有帮助。


I am java developer .I integrated google drive to my application using google app client_id and client_secrect . Based on this client_id i got the access token of the user .After getting the access token.I used the following code to get files and folders from drive.

HttpTransport httpTransport = new NetHttpTransport();
JacksonFactory jsonFactory = new JacksonFactory();
System.out.println(inventoryIntegrationConfig.getAccessToken());

GoogleCredential credential = new GoogleCredential().setAccessToken(inventoryIntegrationConfig.getAccessToken());

Drive service = new Drive.Builder(httpTransport, jsonFactory, null)
.setApplicationName("Akoonu")
.setHttpRequestInitializer(credential).build();

Files.List request = service.files().list();
.
.
.
.

I received file and folder download url and file name successfully .But i can't get the full file path of google drive.Like for example BaseFolder/SubFolder1/Subofsubfolder/testfile.txt. here my expected output is path: BaseFolder/SubFolder1/Subofsubfolder/

I can't get the full file path from any Drive class.Can you you tell me how to get the full file path? Which method we need to use to get full file path?

Please help me here.

解决方案

There is no method to call/get the full file path. First lets clear up some details regarding the full path of the file. Stop thinking of folders and path, and think of the terms like label( parent and collection).

Parents

The IDs of the parent folders which contain the file. If not specified as part of a create request, the file will be placed directly in the My Drive folder. Update requests must use the addParents and removeParents parameters to modify the values.

A file can have more than one parent, each parent in turn can have one or more parent/label/collection. To get the "file path", we need to recursively get the parents of its parent. According to the SO ticket below answered by pinoyyid, "Remember that a file can have multiple parents, each of which can also have multiple parents, thus a file can have multiple paths."

Using Google Drive, disregard the terms path/folder but rather its simply just a property of a file.

Here is an Apps Script that created a workaround on getting the parents/collection of the file and a related SO question for further information. I hope it helps.

这篇关于如何从使用Java的谷歌驱动器获得完整的文件路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆