【问题标题】:Google Drive Api - Get Folder PathsGoogle Drive Api - 获取文件夹路径
【发布时间】:2012-12-04 18:44:18
【问题描述】:

我目前正在使用 Google Drive API 来获取文件夹(或子文件夹)列表。为此,我使用以下 URL:

https://www.googleapis.com/drive/v2/files/1234folderid1234/children?key=1234APIKEY1234&q=mimeType='application/vnd.google-apps.folder';

这会按预期返回文件夹列表,但是,我还需要文件夹名称或完整文件夹路径。此调用不返回此信息。有没有办法在一次调用中获取具有关联文件夹名称或路径的文件夹列表?

谢谢

【问题讨论】:

标签: google-api google-drive-api


【解决方案1】:

你需要做两件事:

  1. 使用files.list(提供完整的文件资源)
  2. 修改您的查询以添加您要搜索的父 ID。

    mimeType = 'application/vnd.google-apps.folder' and 'folderId' in parents

【讨论】:

  • 文件夹似乎没有出现在我的回复中(虽然是常规文件)。知道为什么会这样吗?
  • 我遇到了同样的问题,使用查询帮助了我:FileList result = service.files().list().setQ("'myname@domain.com' in owners").execute();
  • 这方面的另一个很好的查询过滤器mimeType = 'application/vnd.google-apps.folder'
【解决方案2】:

我知道已经很晚了,但解决这个问题的最佳解决方案是使用递归函数来搜索最后一个文件夹父级。 GD API v3 有一个 NodeJS 解决方案,但每个语言和 GD API 版本的推理都是一样的。

const drive = google.drive({version: 'v3', auth});
const rootFolderID = 'TheHighestFolderIDOfYourGoogleDriveAccount';
function getFileOrFolderCompletePath(fileOrFolderID, pathList = [], callback){
    let pathList = [];

    if (fileOrFolderID !== rootFolderID){
        drive.files.get({
            fileId: fileID,
            fields: 'id, parents',
        }, (error, response) => {
            if (!error){
                pathList.push(response.data.parents[0])
                pathList = pathList.reverse();
                getFileOrFolderCompletePath(response.data.parents[0], pathList, callback);
            }
        });
    }else{
        callback(pathList.join('/'))
    }
}

你应该打电话:

getFileOrFolderCompletePath('fileIDtoGetThePath', [], (path) => {
    console.log(path); //It will log: 'path/to/your/file'
})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-30
    • 1970-01-01
    • 2021-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多