[英]Google API Drive: Upload files using node.js but, How use the 1TB storage of a workspace account and not the 15GB of service account used to auth?
我在我的服务器上开发了一个过程,使用 Node.js 将多个文件上传到谷歌驱动器帐户。
首先,我在我的 Google 工作区帐户中购买了额外的存储空间 (1 TB) 以提供足够的空间。 让我们调用 Google 帐户user.account@domain.com
然后,我在user.account@domain.com下创建了一个服务帐户,并从 node.js 创建了身份验证所需的所有凭据。
我已经为模拟过程添加了具有域范围委托的服务 ID 帐户。
我已经定义了所有需要的范围:
https://www.googleapis.com/auth/drive
https://www.googleapis.com/auth/drive.file
https://www.googleapis.com/auth/drive.appdata
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
}
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有足够的空间(1TB 100% 免费),但所有上传的文件仍在服务帐户默认存储中上传,其中一个只有 15GB。
设置上传到所需存储我缺少什么?
谢谢你的帮助!
文档和参考: https://developers.google.com/admin-sdk/directory/v1/guides/delegation
谷歌 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'
},
});
您需要根据您的代码和您正在做的事情来编辑它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.