使用Nodejs将文件上传到Google Team Drive文件夹

时间:2019-03-15 19:19:57

标签: node.js api file-upload google-drive-api google-drive-team-drive

我正在尝试将文件上传到团队驱动器,而我获得的最大成功是它返回了文件ID,但是我在文件夹中看不到该文件,也无法在驱动器中搜索该文件。有人可以告诉我我在做什么错吗?

module.exports.upload = function(req, res, next) {
  let data = req.body.text;
  var fileMetadata = {
    'name': req.body.fileName + '.html',
    "mimeType": "application/vnd.google-apps.document",
    corpora: 'teamDrive',
    supportsTeamDrives: true,
    teamDriveId: TEAM_DRIVE_ID,
    includeTeamDriveItems: true,
    parents: [TEAM_DRIVE_FOLDER_ID]
  };
  let media = {
    mimeType: 'text/html',
    body: data
  };
  drive.files.create({
    auth: token,
    resource: fileMetadata,
    media: media,
    fields: 'id'
  }, function (err, file) {
    if (err) {
      // Handle error
      console.error(err);
    } else {
      console.log('File Id: ', file.data.id);
    }
  });
}

作为测试,我能够成功列出要上传到的文件夹内的文件。因此此代码有效:

module.exports.getFileList = function(req, res, next) {
 drive.files.list({
      auth:token,
      corpora: 'teamDrive',
      supportsTeamDrives: true,
      teamDriveId: process.env.TEAM_DRIVE_ID,
      includeTeamDriveItems: true,
      pageSize: 10,
      q: "'" + TEAM_DRIVE_FOLDER_DI + "' in parents and trashed=false",
      fields: 'files(id,name)',
    }, (err, {data})=> {
    if(err) return console.log('The API returned an error: ' + err);
    var files = data.files;
    if (files.length){
      console.log('Files:');
      files.map((file)=> {
        console.log(`${file.name} (${file.id})`);
      });
    }else{
      console.log('No files found.');
    }
  });
}

我对如何列出文件夹中的文件却不能上传到同一文件夹感到非常困惑。非常感谢您的协助。 谢谢!

0 个答案:

没有答案