简体   繁体   English

使用列表数据为 drive.files.export Google Drive API V3 使用 Nodejs 定义下载参数

[英]Using list data to define download parameters for drive.files.export Google Drive API V3 using Nodejs

My app can successfully list and download (export text/plain) a file, from Google Drive to my local hard drive, using separate functions listFiles() & downloadFile() (extensively using code from Nodejs Quickstart我的应用程序可以使用单独的函数listFiles()downloadFile()成功列出和下载(导出文本/纯文本)文件到我的本地硬盘驱动器(广泛使用来自Nodejs Quickstart的代码

I am trying to combine this code into a function that will download (export text/plain) of all files listed in the folder.我正在尝试将此代码组合到 function 中,它将下载(导出文本/纯文本)文件夹中列出的所有文件。

At this point my file and path references are 'hard coded' in for testing (one file/one path)此时我的文件和路径引用被“硬编码”用于测试(一个文件/一个路径)

So I am trying to understand how any modified listFiles() code could loop through the available list of files and provide the next available fileId as a reference for the downloadFiles() code.因此,我试图了解任何修改后的 listFiles() 代码如何遍历可用的文件列表并提供下一个可用的 fileId 作为 downloadFiles() 代码的参考。 I also wanted to provide the matching fileName for path building.我还想为路径构建提供匹配的文件名。

In my listFiles() I cannot seem to find much information on parsing the returned promise data stream.在我的 listFiles() 中,我似乎找不到太多关于解析返回的 promise 数据 stream 的信息。 So I've just got a dumb version going.所以我有一个愚蠢的版本。 Which can only download 1 file.只能下载1个文件。 (this is my understanding of the code) (这是我对代码的理解)

/**
 * Lists names and IDs of pageSize number of files (using query to define folder of files)
 * @param {google.auth.OAuth2} auth An authorized OAuth2 client.
 */
 function listFiles(auth) {
  const drive = google.drive({version: 'v3', auth});

    drive.files.list({
        corpora: 'user',  
        pageSize: 1,
        // files in a parent folder (drive>ocrTarget ID) that have not been trashed  
        q: `'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' in parents and trashed=false`,    
        fields: 'nextPageToken, files(id, name)',
      }, (err, res) => {
        if (err) return console.log('The API returned an error: ' + err);
        const files = res.data.files;
        if (files.length) {
          files.map((file) => {
            console.log(file);  
          });
        } else {
          console.log('No files found.');
        }
      });
    }

Output Output

PS C:\Users\blah\blah\gDriveDev> node . myNodejsScript
{
  id: 'xxxx file id of only file that could be listed with PageSize: 1 xxxxx',
  name: 'Copy of 31832_226140__0001-00007'
}

I have gone through Google Drive for Developers Drive API (V3) docs (Guides/Reference) Which, cover request parameters.我已经浏览了 Google Drive for Developers Drive API (V3) docs (Guides/Reference) 其中包括请求参数。 However I want to manipulate the data type/structure of output.但是我想操作 output 的数据类型/结构。 eg walk the file list and parse the fileId.例如遍历文件列表并解析fileId。

(i) Before I got my download code working properly I was creating metadata files of a JSON layout type. (i) 在我的下载代码正常工作之前,我正在创建 JSON 布局类型的元数据文件。 Now I have no idea how I did this.现在我不知道我是怎么做到的。 They were saved according to my file/path settings like this:.它们是根据我的文件/路径设置保存的,如下所示:。

{
 "kind": "drive#file",
 "id": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
 "name": "atest.txt",
 "mimeType": "text/plain"
}

(ii) During experiments, running working code, I found that files.length has a value that equals the number of files listed in the available PageSize: (ii) 在运行工作代码的实验期间,我发现 files.length 的值等于可用 PageSize 中列出的文件数:

(iii) For files.map((file) it looked like I was dealing with a Map object MDN Map Reference But error messages in my test code showed that it was not. (iii) 对于files.map((file)看起来我正在处理 Map object MDN Map 参考但是我的测试代码中的错误消息显示它不是。

(iv) I have seen the following type of code used for accessing parameters: (iv) 我见过以下类型的用于访问参数的代码:

let data = 'Name,URL\n';

res.data.files.map(entry => {
  const { name, webViewLink } = entry;
  data += `${name},${webViewLink}\n`;
});

But I don't have the knowledge to interpret this in order to evaluate it for my situation.但是我没有知识来解释这一点,以便根据我的情况对其进行评估。

If anyone can make a suggestion, for my situation, it would be appreciated.如果有人可以就我的情况提出建议,将不胜感激。

-------------------- [Added to question] -------------------- -------------------- [添加到问题] --------------------

To summarize I understand from the following:总结一下,我从以下几点理解:

const files = res.data.files;
    if (files.length) {
       files.map((file) => {
  • length property is the number of files length属性是文件的数量
  • files produces list of length x length (x lists of x files) files产生长度 x 长度的列表(x 个文件的 x 列表)
  • file produces a list of the files (id, name) file生成文件列表(id,名称)

How do I index these results?我如何索引这些结果? Do I have to read each of the file details from the list into an array?我是否必须将列表中的每个文件详细信息读入数组?

Some examples of output here: output 的一些示例在这里:

console.log(Object.keys(file)); // a list of file key types

output output

[ 'id', 'name' ]

and

console.log(file);

eg例如

    {
  id: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
  name: 'Copy of 31832_226140__0001-00007'
}

But the keys are not numbered.但钥匙没有编号。 So there is no numerical reference (index) available.所以没有可用的数字参考(索引)。

My aim: read the full list of file names available and supply these for each file to be downloaded.我的目标:阅读可用文件名的完整列表,并为每个要下载的文件提供这些文件名。

You can use a forEach() loop with an index您可以使用带有索引的forEach()循环

Sample:样本:

const files = res.data.files;
var ids = [];
if (files.length) {
    files.forEach(function (file, i) {
        ids[i]= file.id;
    })
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用drive.files.export(Drive API v3)+ API密钥以文本/纯格式获取公共Google Doc的内容 - Get content of public Google Doc in text/plain format with drive.files.export (Drive API v3) + API key 使用 Google Drive API v3 中的 drive.files.copy 转换,通过 nodejs 使用 API 将 Word 文档转换为 Google 文档 - convert a Word doc into a Google doc using the API via nodejs using drive.files.copy convert in v3 of Google Drive API 无法使用 Google Drive APIs V3 下载文件 - Can not download files using Google Drive APIs V3 Google DRIVE API V3 - 使用nodejs获取根文件夹ID - Google DRIVE API V3 - Get a root folder ID with nodejs Google Drive API v3 files.list 未返回所有文件 - Google Drive API v3 files.list not returning all files Google Drive API v3 查找文件夹和子项中的所有文件 - Google Drive API v3 find all files in folder and children Google Drive API v3 JavaScript:移动文件 - Google Drive API v3 javascript: moving files 使用 Google Drive API v3 将 .docx 或 .doc 文件转换为 google docs 文件 - Using Google Drive API v3 to convert .docx or .doc file to google docs file 使用 Google Drive API v3 将 PDF 文件转换为 Google Doc - Convert a PDF file to Google Doc using Google Drive API v3 谷歌驱动 api v3 更新说明 - google drive api v3 update description
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM