【问题标题】:Uploaded files not found in google drive在谷歌驱动器中找不到上传的文件
【发布时间】:2020-10-10 16:38:10
【问题描述】:

我刚开始使用 google drive,我已成功上传文件并列出了我的所有文件,但我似乎无法在我的 google drive 中找到我上传的文件?

fs.readFile('credentials.json', (err, content) => {
    if (err) return console.log('Error loading credentials', err);
    const credentials = JSON.parse(content);
    const { client_email, private_key } = credentials;

    auth = new google.auth.JWT(
      client_email,
      null,
      private_key,
      SCOPES,
    );
    drive = google.drive({
      version: 'v3',
      auth,
    });
  });

List of files from drive

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

Upload file

const fileMetadata = {
    name: 'test_only', // file name that will be saved in google drive
  };

  const media = {
    mimeType: 'image/png',
    body: fs.createReadStream('image.png'), // Reading the file from our server
  };

    // Uploading Single image to drive
  drive.files.create(
    {
      resource: fileMetadata,
      media: media,
    },
    async (err, file) => {
      console.log(err);
      if (err) {
        // Handle error
        console.error(err);
      } else {
        console.log(file.data.id);
      }
    }
  );

我错过了什么吗?

【问题讨论】:

    标签: javascript node.js google-api


    【解决方案1】:

    我相信你现在的情况如下。

    • 从你的授权脚本中,发现你使用的是服务账号。

        auth = new google.auth.JWT(
          client_email,
          null,
          private_key,
          SCOPES,
        );
      
    • 您可以使用脚本上传文件并检索文件列表。

    修改点:

    • 来自I can't seem to find my uploaded files in my google drive,我认为您的问题的原因是使用服务帐户上传文件。在这种情况下,服务帐户的 Drive 与您的 Google Drive 不同。这样,上传的文件将无法显示在您的 Google Drive 中。而且,服务帐号的Drive不能直接用浏览器显示。

    作为在浏览器中显示上传文件的解决方法,我想要以下解决方法。

    解决方法 1:

    在此解决方法中,您的 Google Drive 中的一个文件夹与服务帐户的电子邮件共享。并将上传的文件放入共享文件夹。这样,您就可以使用 Google Drive 中的服务帐户查看上传的文件。

    解决方法 2:

    在此解决方法中,服务帐户中的一个文件夹与您的 Google 帐户的电子邮件共享。并将上传的文件放入共享文件夹。这样,您就可以在浏览器中使用服务帐号查看上传的文件了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多