【问题标题】:Get the connection between files and folders in GDrive获取 GDrive 中文件和文件夹之间的连接
【发布时间】:2019-11-30 10:17:44
【问题描述】:

我有一个简单的 Js 方法,可以打印出我的 Google Drive 的内容。我弄乱了 Google 提供的示例,我可以随意打印某些文件夹和文件。

但我想知道是否有可能以某种方式查看来自哪个文件夹的文件。目前我只列出了文件和文件夹,还有 id 和 mime 类型,但我看不到任何属性或方法来确保哪个文件来自哪个文件夹。

有没有可能?

我的示例代码:

    /**
     * @param {google.auth.OAuth2} auth An authorized OAuth2 client.
     */
    function listFiles(auth) {
        const drive = google.drive({version: 'v3', auth});
        drive.files.list({}, (err, res) => {
            if (err) throw err;

            const files = res.data.files;
            if (files.length) {
                files.map((file) => {
                    console.log(file);
                });
            } else {
                console.log('No files found');
            }
        });
    }

这会返回:

    { 
        kind: 'drive#file',
        id: 'example-id',
        name: 'example-file-name',
        mimeType: 'application/vnd.google-apps.folder' 
    }

【问题讨论】:

    标签: javascript node.js google-drive-api


    【解决方案1】:
    • drive.files.list()检索文件列表时,要检索文件的父文件夹信息。
    • 您希望通过 Node.js 使用 googleapis 来实现此目的。
    • 您已经可以使用drive.files.list()

    如果我的理解是正确的,那么这个答案呢?请认为这只是几个可能的答案之一。

    修改点:

    • parents的参数用于drive.files.list()fields时,可以获取文件的父文件夹ID。这样就可以获取文件的父文件夹信息了。

    修改脚本:

    当你的脚本被修改后,变成如下。

    drive.files.list(
      {
        fields: "files(id, kind, name, mimeType, parents)"  // Added
      },
      (err, res) => {
        if (err) throw err;
    
        const files = res.data.files;
        if (files.length) {
          files.map(file => {
            console.log(file);
          });
        } else {
          console.log("No files found");
        }
      }
    );
    

    注意:

    • 在此修改中,将检索父 ID。如果要检索父文件夹名称,请使用drive.files.get()。或者在使用drive.files.list()检索到包含文件夹ID和文件夹名称的文件夹列表后,请从文件夹列表中检索父文件夹名称。
    • 当然,你也可以用fields: "*"代替fields: "files(id, kind, name, mimeType, parents)"。在这种情况下,这意味着检索到所有字段。

    参考资料:

    如果我误解了您的问题并且这不是您想要的方向,我深表歉意。

    【讨论】:

    • 是的,这行得通。我什至没有考虑过有父母的可能性。非常感谢,让它按我的意愿工作。
    • @Kertix 感谢您的回复。我很高兴你的问题得到了解决。对了,我加了一个“node.js”的标签。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多