google 驱动 api 上传成功 return id.但驱动器中不存在文件?

google drive api upload success return id. but file not exist in drive?

使用 return id 上传成功。并尝试 drive.files.list 。它存在。但驱动器中不存在文件?这是什么?我尝试通过 id 上传到特定文件夹。文件上传位置在哪里?

const { google } = require('googleapis');
const fs = require('fs');
const credentials = require('./credentials.json');
const scopes = [
    'https://www.googleapis.com/auth/drive'
];

const auth = new google.auth.JWT(
    credentials.client_email, null,
    credentials.private_key, scopes
);

const drive = google.drive({ version: 'v3', auth });

(async () => {
    
    const fileMetadata = {
        name: 'naruto.jpg'
    };

    const media = {
        mimeType: 'image/jpeg',
        body: fs.createReadStream('naruto.jpg')
    };

    const res = await drive.files.create({
        q: `'1PhND1fjEDnL63BbrLmw2V4M4B6jIfP8Q' in parents`,
        resource: fileMetadata,
        media: media,
        fields: 'id'
    });

    console.log(res.data);
    
})();

您似乎在使用服务帐户。服务帐户有自己的 google 驱动器帐户,在您的情况下,您似乎已将文件上传到服务帐户 google 驱动器帐户。

如果您想将文件上传到您自己的个人 google 驱动器帐户,您应该在您的个人驱动器帐户上与服务帐户共享一个目录,然后在创建文件元数据时确保设置您希望将文件上传到的父目录。

let fileMetadata = {
        'name': 'icon.png',
        'parents':  [  '10krlloIS2i_2u_ewkdv3_1NqcpmWSL1w'  ]
    };

代码也从 Upload Image to Google drive with Node Js 中窃取 Google drive API upload file with Nodejs + service account