node.jsgoogle-cloud-platformgoogle-cloud-storagegoogle-api-nodejs-clientgoogle-auth-library

How to use google-auth-library with Google Cloud Storage with NodeJS?


I want to use Google Cloud Storage in NodeJS but authenticate with google-auth-library Specifically: I want to host this on heroku, so I want to keep the secret in an environment variable, not in a file (as I'd have to commit the file to deploy to heroku). Basically what is suggested in the auth library: https://github.com/googleapis/google-auth-library-nodejs#loading-credentials-from-environment-variables

But I can't seem to pass the resulting client to the Storage constructor?


Solution

  • Reading the code [1,2,3,4,5] you should be able to pass in credentials as constructor options:

    storageOptions = {
      projectId: 'your-project-id',
      credentials: {
            client_email: 'your-client-email',
            private_key: 'your-private-key'
      }
    };
    client = new Storage(storageOptions);