Google 驱动器 API: 无法使用 nodejs 将文件上传到根文件夹
Google drive API: can't upload files to the root folder using nodejs
我正在使用 google 驱动器 API 上传文件。
如果我指定子文件夹的 ID,它就可以工作。但是当我传递根文件夹的 id 时,文件没有上传。
这是我的要求:
drive.files.create(
{
auth: this.ggToken,
fields: 'id',
supportsAllDrives: true,
media: {
body: this.convertBufferToStream(file.buffer),
},
requestBody: {
mimeType: file.mimetype,
name: file.originalname,
parents: ['root', '0AASRHiHHtzxrUk9PVA'],
},
},
(e: Error, f: any) => {
if (e) {
console.error(e);
}
console.log(f);
},
);
0AASRHiHHtzxrUk9PVA
是根文件夹的ID(我是用drive.files.getAPI得到的)
那么我的代码有什么问题呢?如何将文件上传到根文件夹?
谢谢。
更新 1
这是我的脚本:
ggToken: JWT;
constructor() {
this.ggToken = new google.auth.JWT(
process.env.GG_DRIVE_CLIENT_EMAIL,
null,
process.env.GG_DRIVE_PRIVATE_KEY,
['https://www.googleapis.com/auth/drive'],
null,
);
}
async uploadFiles(file: any) {
const credentials = await this.ggToken.authorize();
this.ggToken.setCredentials(credentials);
const uploadedFile = await drive.files.create({
auth: this.ggToken,
fields:
'id, name, mimeType, webViewLink, webContentLink, iconLink, size, originalFilename',
media: {
body: this.convertBufferToStream(file.buffer),
},
requestBody: {
mimeType: file.mimetype,
name: file.originalname,
parents: ['root'],
properties: {},
},
});
return {
driveResource: uploadedFile.data,
};
}
创建服务器帐户密钥后,我从 json 文件中获得了 process.env.GG_DRIVE_CLIENT_EMAIL 和 process.env.GG_DRIVE_PRIVATE_KEY。
非常感谢@Tanaike。
这是他的回答:
Thank you for replying and adding the script. I could understand about the reason of your issue. The service account is different from your own account. So the Google Drive of service account is different from that of your account. By this, when a file is uploaded to the root folder using your script, the file is uploaded to the root folder of the Drive of service account. By this, the file cannot be seen at the drive of your account. If you want to upload the file to the root folder of the Drive of your account, please use OAuth2
使用 Oauth2,我可以将文件上传到根文件夹。
这是我的代码:
oauth2Client: OAuth2Client;
constructor() {
this.oauth2Client = new google.auth.OAuth2({
clientId: process.env.CLIENT_ID,
clientSecret: process.env.CLIENT_SECRET,
redirectUri: 'https://developers.google.com/oauthplayground',
});
this.oauth2Client.setCredentials({
access_token: process.env.ACCESS_TOKEN,
refresh_token: process.env.REFRESH_TOKEN,
});
}
async uploadFiles(file: any) {
const uploadedFile = await drive.files.create({
auth: this.oauth2Client,
fields:
'id, name, mimeType, webViewLink, webContentLink, iconLink, size, originalFilename',
media: {
body: this.convertBufferToStream(file.buffer),
},
requestBody: {
mimeType: file.mimetype,
name: file.originalname,
parents: ['root'],
},
});
return {
driveResource: uploadedFile.data,
};
}
我正在使用 google 驱动器 API 上传文件。 如果我指定子文件夹的 ID,它就可以工作。但是当我传递根文件夹的 id 时,文件没有上传。 这是我的要求:
drive.files.create(
{
auth: this.ggToken,
fields: 'id',
supportsAllDrives: true,
media: {
body: this.convertBufferToStream(file.buffer),
},
requestBody: {
mimeType: file.mimetype,
name: file.originalname,
parents: ['root', '0AASRHiHHtzxrUk9PVA'],
},
},
(e: Error, f: any) => {
if (e) {
console.error(e);
}
console.log(f);
},
);
0AASRHiHHtzxrUk9PVA
是根文件夹的ID(我是用drive.files.getAPI得到的)
那么我的代码有什么问题呢?如何将文件上传到根文件夹?
谢谢。
更新 1 这是我的脚本:
ggToken: JWT;
constructor() {
this.ggToken = new google.auth.JWT(
process.env.GG_DRIVE_CLIENT_EMAIL,
null,
process.env.GG_DRIVE_PRIVATE_KEY,
['https://www.googleapis.com/auth/drive'],
null,
);
}
async uploadFiles(file: any) {
const credentials = await this.ggToken.authorize();
this.ggToken.setCredentials(credentials);
const uploadedFile = await drive.files.create({
auth: this.ggToken,
fields:
'id, name, mimeType, webViewLink, webContentLink, iconLink, size, originalFilename',
media: {
body: this.convertBufferToStream(file.buffer),
},
requestBody: {
mimeType: file.mimetype,
name: file.originalname,
parents: ['root'],
properties: {},
},
});
return {
driveResource: uploadedFile.data,
};
}
创建服务器帐户密钥后,我从 json 文件中获得了 process.env.GG_DRIVE_CLIENT_EMAIL 和 process.env.GG_DRIVE_PRIVATE_KEY。
非常感谢@Tanaike。 这是他的回答:
Thank you for replying and adding the script. I could understand about the reason of your issue. The service account is different from your own account. So the Google Drive of service account is different from that of your account. By this, when a file is uploaded to the root folder using your script, the file is uploaded to the root folder of the Drive of service account. By this, the file cannot be seen at the drive of your account. If you want to upload the file to the root folder of the Drive of your account, please use OAuth2
使用 Oauth2,我可以将文件上传到根文件夹。 这是我的代码:
oauth2Client: OAuth2Client;
constructor() {
this.oauth2Client = new google.auth.OAuth2({
clientId: process.env.CLIENT_ID,
clientSecret: process.env.CLIENT_SECRET,
redirectUri: 'https://developers.google.com/oauthplayground',
});
this.oauth2Client.setCredentials({
access_token: process.env.ACCESS_TOKEN,
refresh_token: process.env.REFRESH_TOKEN,
});
}
async uploadFiles(file: any) {
const uploadedFile = await drive.files.create({
auth: this.oauth2Client,
fields:
'id, name, mimeType, webViewLink, webContentLink, iconLink, size, originalFilename',
media: {
body: this.convertBufferToStream(file.buffer),
},
requestBody: {
mimeType: file.mimetype,
name: file.originalname,
parents: ['root'],
},
});
return {
driveResource: uploadedFile.data,
};
}