So I have the service account credentials json (as a string, or json object in my code):
{
"type": "service_account",
"project_id": "myproject",
"private_key_id": "lkjshdjhskas",
//.... etc
}
and then I have this simple Cloud Storage upload snippet:
const {Storage} = require('@google-cloud/storage');
const creds = {service_account_object:"creds are here"};
const storage = new Storage();
const bucketName = 'my-bucket';
const filename = 'my-file.txt';
storage.bucket(bucketName).upload(filename, {
gzip: true,
metadata: {
cacheControl: 'public, max-age=31536000',
},
});
This is gives me an invalid_grant error, of course:
(node:15920) UnhandledPromiseRejectionWarning: Error: invalid_grant
at Gaxios.request (C:\Users\Yuri\Documents\Code\btests\node_modules\gaxios\build\src\gaxios.js:70:23)
at process._tickCallback (internal/process/next_tick.js:68:7)
(node:15920) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:15920) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
I tried providing the creds as a param to the Storage()
constructor, lie this Storage(creds)
or even Storage(JSON.stringify(creds))
but it just gives me weird errors.
How do instantiate a Google client library object with service account credentials?
The credentials data should be passed in the credentials
property of the StorageOptions object that you pass to the constructor:
const creds = {
"type": "service_account",
"project_id": "myproject",
"private_key_id": "lkjshdjhskas",
//.... etc
}
const storage = new Storage({
credentials: creds
});
Definitely familiarize yourself with the linked API docs, as they will explain how to use the APIs.