LoginSignup
2
2

More than 5 years have passed since last update.

Downloading files from google cloud storage using service account authentication with nodejs google-api-nodejs-client

Last updated at Posted at 2014-09-09

Accessing Google API using Service Account in Node.js was quite helpful for me but it was little bit out of date(due to written based on older api library).

I put my version of snippet which works with latest nodejs client library. Hope it helps :sparkles:

#!/usr/bin/env coffee
googleapis = require('googleapis')

email = SERVICE_ACCOUNT_EMAIL
# see https://developers.google.com/storage/docs/authentication#converting-the-private-key for how to convert .p12 to .pem
keypath = SERVICE_ACCOUNT_PEM
key = null

storage = googleapis.storage('v1')

# see https://developers.google.com/storage/docs/authentication
# for auth url
authClient = new googleapis.auth.JWT(email, keypath, key, ['https://www.googleapis.com/auth/devstorage.read_only'] )

authClient.authorize (err, tokens) ->
  if (err)
    console.log(err)
    return

  storage.objects.get {
    auth: authClient,
    bucket: 'billing-exports',
    object: 'bill-2014-09-08.json?alt=media' # add '?alt=media' to download the file itself.
  }, (err, file) ->
    console.log err, file

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2