Google API驱动器:使用node.js上载文件,但是,如何使用工作区帐户的1TB存储,而不是用于验证的15GB服



我在服务器上使用Node.js开发了一个进程,用于将多个文件上传到谷歌驱动器帐户。

  1. 首先,我在谷歌工作区帐户中购买了额外的存储空间(1TB(,以获得足够的空间。让我们调用谷歌帐户user.account@domain.com

  2. 然后,我在下创建了一个服务帐户user.account@domain.com以及从node.js.进行身份验证所需的所有凭据

  3. 我已经添加了具有用于模拟进程的域范围委派的服务id帐户。

  4. 我已经定义了所有需要的范围:

https://www.googleapis.com/auth/drive
https://www.googleapis.com/auth/drive.file
https://www.googleapis.com/auth/drive.appdata
  1. 最后,我在JavaScript实现中添加了所有需要的内容。下一个代码是来自JavaScript实现的auth Google API的唯一声明:
const { google } = require('googleapis');
// service account key file from Google Cloud console.
const KEYFILEPATH = 'upload-files-server-to-drive-7a5e1bf2dcbe.json';
// Request full drive access.
const SCOPES = ['https://www.googleapis.com/auth/drive','https://www.googleapis.com/auth/drive.file','https://www.googleapis.com/auth/drive.appdata'];
// Create a service account initialize with the service account key file and scope needed
const auth = new google.auth.GoogleAuth({
keyFile: KEYFILEPATH,
scopes: SCOPES
});
auth.subject = 'user.account@domain.com';
const driveService = google.drive({version: 'v3', auth});
module.exports = {
driveService
}
  1. 下一个函数在模块内,上传发生在这里:
const upload = async ( params ) => new Promise (async ( resolve, reject ) => {
const driveService = params.driveService;
const fileMetadata = {
'name': `${params.id_numero}${params.name}`,
'parents':  [  params.parentId  ]
};

try {
if (fs.existsSync(params.folder + params.name)) {
//file exists
}else{
reject({
result: "error",
message:"El archivo no existe. No se puede subir nada. " 
})
return
}
} catch(err) {
console.log(err)
reject({
result: "error",
message: "Hubo un error" . err 
})
return
}
// Definición del archivo que se desea subir
const  media = {
mimeType: params.mimeType,
body: fs.createReadStream(params.folder + params.name)
};
await driveService.files.create({
resource: fileMetadata,
media: media,
fields: 'id'
})
.then( response => {
console.log(response)
switch(response.status){
case 200:
let file = response.result;
resolve({
result: "success",
message:'Created File Id: ' +  response.data.id,
url : "http://drive.google.com/uc?export=view&id="+response.data.id,
id : response.data.id
})
break;
default:
console.error('Error creating the file, ' + response.errors);
reject({
result: "error",
message:"Error al crear el archivo en Drive, " + response.errors
})
break;
}
})
.catch( e => {
console.log(e)
reject({
result: "error",
message:"ERROR. No se puede acceder a DRIVE " ,
error : e.errors
})
return
})
})

上面的代码完全适用于将文件上传到驱动器。然而,尽管user.account@domain.com有足够的空间(100%可用1TB(,所有上传的文件仍在服务帐户默认存储中上传,其中只有15GB。

设置为上传到所需的存储设备缺少什么?

谢谢你的帮助!

文件和参考资料:https://developers.google.com/admin-sdk/directory/v1/guides/delegation

Google API范围:https://developers.google.com/drive/api/v3/reference/files/create#auth

对于模拟,您可以尝试:

const auth = new google.auth.GoogleAuth({
keyFile: './service.json',
scopes: [
'https://www.googleapis.com/auth/contacts',...
],
clientOptions: {
subject: 'email@email.com'
},
});

你需要根据你的代码和你正在做的事情来编辑它。

最新更新