Google-api-nodejs-client: How to create Node.js Rest Api for uploading file to Google Drive using Angular

Created on 24 Apr 2019  路  4Comments  路  Source: googleapis/google-api-nodejs-client

Hi, i can upload using drive samples to Google Drive. But i want to upload this files from Angular. How can i create an rest api method in node.js for uploading files to google drive ? How to handle auth ?
I find a similar question but not useful answer.

question web

Most helpful comment

@buseodaci,
You have to use Service account for authorization. You will also need domain wide delegation on the service account if you want to upload the doc as any user from the domain and the scopes will need to be authorized(https://developers.google.com/admin-sdk/directory/v1/guides/delegation#delegate_domain-wide_authority_to_your_service_account)
Try the code below.

var Multer  = require('multer');
const multer = Multer({
  storage: Multer.MemoryStorage,
  limits: {
    fileSize: 5 * 1024 * 1024 // no larger than 5mb
  }
});
app.post('/upload', multer.single('my file'), (req, res, next) => {
  const { google } = require('googleapis');
  const stream = require('stream');
  const serviceAccount = 'PATH TO SERVICE ACCOUNT';  
  let fileObject = req.file;
  let bufferStream = new stream.PassThrough();
  bufferStream.end(fileObject.buffer);
    const jWTClient = new google.auth.JWT(
      serviceAccount.client_email,
      null,
      serviceAccount.private_key,
      ['<COMMA SEPARATED SCOPES WHICH ARE AUTHORIZED>']
    )
    google.drive({ version: 'v3'})
        .files.create({
            auth: jWTClient,
            media: {
                mimeType: 'application/pdf',
                body: bufferStream
            },
            resource: {
                name: 'test.pdf',
                // if you want to store the file in the root, remove this parents
                parents: ['Drive folder id in which the file needs to be uploaded.']
            },
            fields: 'id',
        }).then(function (resp) {
            console.log(resp,'resp');
        }).catch(function (error) {
            console.log(error);
        })
    res.send('File uploaded');
});

Do share what kind of uploading you're implementing.
whether you need to implement uploading to Google Drive from a single user or from any user in the domain or logged in user. Basis on this need, the auth may change.

Also, You'll encounter issues with stream if you're using NodeJS 8.x.x. DOn't forget to upgrade NodeJS to 10.x.x for the seamless implementation.

All 4 comments

Hi, @buseodaci can you please add some information. Are you uploading directly from your node.js server to drive or directly from Angular to google drive?

@vishald123 I want to upload doc file over using Angular UI and Node.js Rest API for uploading to Google Drive. So my issue is how to create node.js rest api for uploading files to google drive ?

@buseodaci,
You have to use Service account for authorization. You will also need domain wide delegation on the service account if you want to upload the doc as any user from the domain and the scopes will need to be authorized(https://developers.google.com/admin-sdk/directory/v1/guides/delegation#delegate_domain-wide_authority_to_your_service_account)
Try the code below.

var Multer  = require('multer');
const multer = Multer({
  storage: Multer.MemoryStorage,
  limits: {
    fileSize: 5 * 1024 * 1024 // no larger than 5mb
  }
});
app.post('/upload', multer.single('my file'), (req, res, next) => {
  const { google } = require('googleapis');
  const stream = require('stream');
  const serviceAccount = 'PATH TO SERVICE ACCOUNT';  
  let fileObject = req.file;
  let bufferStream = new stream.PassThrough();
  bufferStream.end(fileObject.buffer);
    const jWTClient = new google.auth.JWT(
      serviceAccount.client_email,
      null,
      serviceAccount.private_key,
      ['<COMMA SEPARATED SCOPES WHICH ARE AUTHORIZED>']
    )
    google.drive({ version: 'v3'})
        .files.create({
            auth: jWTClient,
            media: {
                mimeType: 'application/pdf',
                body: bufferStream
            },
            resource: {
                name: 'test.pdf',
                // if you want to store the file in the root, remove this parents
                parents: ['Drive folder id in which the file needs to be uploaded.']
            },
            fields: 'id',
        }).then(function (resp) {
            console.log(resp,'resp');
        }).catch(function (error) {
            console.log(error);
        })
    res.send('File uploaded');
});

Do share what kind of uploading you're implementing.
whether you need to implement uploading to Google Drive from a single user or from any user in the domain or logged in user. Basis on this need, the auth may change.

Also, You'll encounter issues with stream if you're using NodeJS 8.x.x. DOn't forget to upgrade NodeJS to 10.x.x for the seamless implementation.

Thank you.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ACMerriman picture ACMerriman  路  3Comments

hainguyents13 picture hainguyents13  路  3Comments

lowagner picture lowagner  路  3Comments

skiod picture skiod  路  3Comments

JustinBeckwith picture JustinBeckwith  路  3Comments