我只是从Google驱动器开始,已经成功上传了文件并列出了所有文件,但是我似乎无法在Google驱动器中找到上传的文件?
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);
}
}
);
我错过了什么?
相信您目前的情况如下。
从授权脚本中,发现您正在使用服务帐户。
auth = new google.auth.JWT(
client_email,
null,
private_key,
SCOPES,
);
您可以使用脚本上载文件并检索文件列表。
I can't seem to find my uploaded files in my google drive
,我认为您遇到问题的原因是使用服务帐户上传文件。在这种情况下,服务帐户的云端硬盘与您的Google云端硬盘不同。这样,上传的文件将无法显示在您的Google云端硬盘中。而且,服务驱动器帐户无法直接在浏览器中显示。作为一种在浏览器中显示上载文件的解决方法,我想采用以下解决方法。
在这种解决方法中,您的Google云端硬盘中的文件夹与服务帐户的电子邮件共享。并将上传的文件放入共享文件夹。这样,您可以使用Google云端硬盘中的服务帐户查看上传的文件。
在这种解决方法中,服务帐户中的文件夹与您的Google帐户的电子邮件共享。并将上传的文件放入共享文件夹。这样,您可以使用浏览器中的服务帐户查看上载的文件。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句