'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?
I've developed a process on my server using Node.js for uploading several files to a google drive account.
First at all, I bought additional storage (1 TB) in my Google workspace account for having enough space. Let's call the Google account [email protected]
Then, I've created a service account under [email protected] and all credentials needed for authentication from node.js.
I've added the service id account with a Domain wide delegation for impersonation process.
I've defined all scopes supposed needed:
https://www.googleapis.com/auth/drive
https://www.googleapis.com/auth/drive.file
https://www.googleapis.com/auth/drive.appdata
- And finally, I've added to JavaScript implementation all supposed needed too. The next code is only declaration of auth Google API from JavaScript implementation:
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 = '[email protected]';
const driveService = google.drive({version: 'v3', auth});
module.exports = {
driveService
}
- The next function is inside a module and here is where the uploads occurs:
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
})
})
The code above definitively works for uploading files to drive. However, although even [email protected] has enough space (100% free of 1TB) all uploaded files still being uploaded on the service account default storage, which one is only 15GB.
What am I missing for setting to upload on the desired storage?
Thanks for any help!
Documentation and references: https://developers.google.com/admin-sdk/directory/v1/guides/delegation
Google API scopes: https://developers.google.com/drive/api/v3/reference/files/create#auth
Solution 1:[1]
For the impersonation you could try:
const auth = new google.auth.GoogleAuth({
keyFile: './service.json',
scopes: [
'https://www.googleapis.com/auth/contacts',...
],
clientOptions: {
subject: '[email protected]'
},
});
You would need to edit it depending on your code and what you are doing.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Fernando Lara |
