使用 google 驱动器 api 上传 excel 文件
Upload a excel file with google drive api
我的 google 驱动器 API 有问题。
我正在尝试使用此 API 上传一个 excel 文件,但无法正常工作。即使复制 google API 文档也不起作用。
这是我的代码示例:
@Get('teste')
async teste(){
const keys = require(path.resolve('src', 'files', 'api', 'keys'))
const client = new google.auth.JWT(
keys.client_email,
null,
keys.private_key,
['https://www.googleapis.com/auth/drive.metadata.readonly']
)
client.authorize((err, tokens) =>{
if(err){
console.log(err)
return;
} else{
this.gdrun(client)
}
})
}
gdrun(client){
const drive = google.drive({version: 'v3', auth: client});
var fileMetadata = {
name: 'My Report',
mimeType: 'application/vnd.google-apps.spreadsheet'
};
var media = {
mimeType: 'application/vnd.ms-excel',
body: require(path.resolve('src', 'files', 'excel', 'solargroup.xlsx'))
};
drive.files.create({
resource: fileMetadata,
media: media,
fields: 'id'
}, function (err, file: any) {
if (err) {
// Handle error
console.error(err);
} else {
console.log('File Id:', file.id);
}
});
}
我收到这个错误:
我相信你的目标如下。
- 您想将文件(XLSX 文件)上传到服务帐户的 Google 驱动器。
- 您想使用带有 googleapis 的服务帐户实现此目的 Node.js。
- 根据您的脚本,我认为您可能希望通过转换将 XLSX 文件上传为 Google 电子表格。
修改点:
- 当你想上传文件到Google驱动器时,在这种情况下,请使用
https://www.googleapis.com/auth/drive
的范围而不是https://www.googleapis.com/auth/drive.metadata.readonly
。
- 当您想上传XLSX文件作为XLSX文件时,mimeType为
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
。
- 上传文件时,
body: require(path.resolve('src', 'files', 'excel', 'solargroup.xlsx'))
不可用。在这种情况下,请使用 body: fs.createReadStream(path.resolve('src', 'files', 'excel', 'solargroup.xlsx'))
。我认为您的错误消息可能是由于此造成的。
- 当您要检索上传文件的文件ID时,请将
file.id
修改为file.data.id
。
当以上几点反映到你的脚本中,就变成了下面这样。
修改后的脚本:
发件人:
const client = new google.auth.JWT(
keys.client_email,
null,
keys.private_key,
['https://www.googleapis.com/auth/drive.metadata.readonly']
)
收件人:
const client = new google.auth.JWT(
keys.client_email,
null,
keys.private_key,
['https://www.googleapis.com/auth/drive'] // Modified
)
另外,请修改您的gdrun()
如下。
gdrun(client){
const drive = google.drive({ version: "v3", auth: client });
var fileMetadata = {
name: "My Report",
mimeType: "application/vnd.google-apps.spreadsheet",
};
var media = {
mimeType: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", // Modified
body: fs.createReadStream(path.resolve('src', 'files', 'excel', 'solargroup.xlsx')), // Modified
};
drive.files.create(
{
resource: fileMetadata,
media: media,
fields: "id",
},
function (err, file) {
if (err) {
console.error(err);
} else {
console.log("File Id:", file.data.id); // Modified
}
}
);
}
- 这种情况下,请使用
const fs = require("fs")
。
结果:
当上述脚本为运行时,得到如下结果
File Id: ###fileId###
注:
您的脚本将 XLSX 文件作为 Google 电子表格上传到服务帐户的 Google 驱动器。在这种情况下,您无法在 Google 驱动器中直接看到上传的文件。因为服务帐号的 Google Drive 和你的 Google Drive 不一样。当您想在 Google 驱动器上查看上传的文件时,请在 Google 驱动器上创建一个文件夹,并将创建的文件夹与服务帐户的电子邮件共享。然后,请将文件上传到共享文件夹。这样,您就可以使用浏览器在 Google 驱动器上看到上传的文件。为此,请修改fileMetadata
如下。
var fileMetadata = {
name: "My Report",
mimeType: "application/vnd.google-apps.spreadsheet",
parents: ["### folderId ###"], // Please set the folder ID of the folder shared with the service account.
};
在上面的脚本中,最大文件大小为 5 MB。请注意这一点。当您要上传超过 5 MB 的文件时,请使用断点续传。 Ref
参考文献:
我的 google 驱动器 API 有问题。
我正在尝试使用此 API 上传一个 excel 文件,但无法正常工作。即使复制 google API 文档也不起作用。
这是我的代码示例:
@Get('teste')
async teste(){
const keys = require(path.resolve('src', 'files', 'api', 'keys'))
const client = new google.auth.JWT(
keys.client_email,
null,
keys.private_key,
['https://www.googleapis.com/auth/drive.metadata.readonly']
)
client.authorize((err, tokens) =>{
if(err){
console.log(err)
return;
} else{
this.gdrun(client)
}
})
}
gdrun(client){
const drive = google.drive({version: 'v3', auth: client});
var fileMetadata = {
name: 'My Report',
mimeType: 'application/vnd.google-apps.spreadsheet'
};
var media = {
mimeType: 'application/vnd.ms-excel',
body: require(path.resolve('src', 'files', 'excel', 'solargroup.xlsx'))
};
drive.files.create({
resource: fileMetadata,
media: media,
fields: 'id'
}, function (err, file: any) {
if (err) {
// Handle error
console.error(err);
} else {
console.log('File Id:', file.id);
}
});
}
我收到这个错误:
我相信你的目标如下。
- 您想将文件(XLSX 文件)上传到服务帐户的 Google 驱动器。
- 您想使用带有 googleapis 的服务帐户实现此目的 Node.js。
- 根据您的脚本,我认为您可能希望通过转换将 XLSX 文件上传为 Google 电子表格。
修改点:
- 当你想上传文件到Google驱动器时,在这种情况下,请使用
https://www.googleapis.com/auth/drive
的范围而不是https://www.googleapis.com/auth/drive.metadata.readonly
。 - 当您想上传XLSX文件作为XLSX文件时,mimeType为
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
。 - 上传文件时,
body: require(path.resolve('src', 'files', 'excel', 'solargroup.xlsx'))
不可用。在这种情况下,请使用body: fs.createReadStream(path.resolve('src', 'files', 'excel', 'solargroup.xlsx'))
。我认为您的错误消息可能是由于此造成的。 - 当您要检索上传文件的文件ID时,请将
file.id
修改为file.data.id
。
当以上几点反映到你的脚本中,就变成了下面这样。
修改后的脚本:
发件人:
const client = new google.auth.JWT(
keys.client_email,
null,
keys.private_key,
['https://www.googleapis.com/auth/drive.metadata.readonly']
)
收件人:
const client = new google.auth.JWT(
keys.client_email,
null,
keys.private_key,
['https://www.googleapis.com/auth/drive'] // Modified
)
另外,请修改您的gdrun()
如下。
gdrun(client){
const drive = google.drive({ version: "v3", auth: client });
var fileMetadata = {
name: "My Report",
mimeType: "application/vnd.google-apps.spreadsheet",
};
var media = {
mimeType: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", // Modified
body: fs.createReadStream(path.resolve('src', 'files', 'excel', 'solargroup.xlsx')), // Modified
};
drive.files.create(
{
resource: fileMetadata,
media: media,
fields: "id",
},
function (err, file) {
if (err) {
console.error(err);
} else {
console.log("File Id:", file.data.id); // Modified
}
}
);
}
- 这种情况下,请使用
const fs = require("fs")
。
结果:
当上述脚本为运行时,得到如下结果
File Id: ###fileId###
注:
您的脚本将 XLSX 文件作为 Google 电子表格上传到服务帐户的 Google 驱动器。在这种情况下,您无法在 Google 驱动器中直接看到上传的文件。因为服务帐号的 Google Drive 和你的 Google Drive 不一样。当您想在 Google 驱动器上查看上传的文件时,请在 Google 驱动器上创建一个文件夹,并将创建的文件夹与服务帐户的电子邮件共享。然后,请将文件上传到共享文件夹。这样,您就可以使用浏览器在 Google 驱动器上看到上传的文件。为此,请修改
fileMetadata
如下。var fileMetadata = { name: "My Report", mimeType: "application/vnd.google-apps.spreadsheet", parents: ["### folderId ###"], // Please set the folder ID of the folder shared with the service account. };
在上面的脚本中,最大文件大小为 5 MB。请注意这一点。当您要上传超过 5 MB 的文件时,请使用断点续传。 Ref