代码之家  ›  专栏  ›  技术社区  ›  blitzen12

在谷歌驱动器中找不到上传的文件

  •  0
  • blitzen12  · 技术社区  · 4 年前

    我刚开始使用谷歌硬盘,我已经成功上传了文件并列出了我的所有文件,但我似乎在谷歌硬盘中找不到我上传的文件?

    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);
          }
        }
      );
    

    Results on list of files

    我错过什么了吗?

    0 回复  |  直到 4 年前
        1
  •  1
  •   Tanaike    4 年前

    我认为你目前的情况如下。

    • 从您的授权脚本中可以发现您正在使用服务帐户。

        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云端硬盘中显示。此外,服务帐户的驱动器不能直接在浏览器中显示。

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

    解决方法1:

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

    解决方法2:

    在此解决方法中,服务帐户中的一个文件夹与您的Google帐户的电子邮件共享。上传的文件被放入共享文件夹。通过这种方式,您可以在浏览器中使用服务帐户查看上传的文件。