Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Cloud storage is not a function

I am trying to connect to the Google Cloud Bucket through

 const storage = require('@google-cloud/storage');
 const gcs = storage({    //TypeError: storage is not a function
  "keyFileName": 'path-to-keyfile.json',
  "type": "service_account",
  "project_id": <PROJECT_NAME>,

 //SOME CREDENTIALS
});

const bucket = gcs.bucket(<BUCKET_NAME>)

but I am getting an error that storage is not a function. Is there some issue that i am missing?

like image 285
anubysh Avatar asked Jan 02 '19 12:01

anubysh


People also ask

What is the function of Google cloud?

Google Cloud Functions is a serverless execution environment for building and connecting cloud services. With Cloud Functions you write simple, single-purpose functions that are attached to events emitted from your cloud infrastructure and services. Your function is triggered when an event being watched is fired.


2 Answers

By using the Node.js Cloud Storage client libraries version 2.3.4 I was able to connect to a bucket with this code:

'use strict';

async function quickstart(
  projectId = 'PROJECT_ID', // Your Google Cloud Platform project ID
  keyFilename = '/home/folder/key.json' //Full path of the JSON file
) {
  // Imports the Google Cloud client library
  const {Storage} = require('@google-cloud/storage');

  // Creates a client
  const storage = new Storage({keyFilename,projectId});
  const bucket = storage.bucket('BUCKET_NAME')

This was based on the Quickstart documentation and the constructor options

Hope it helps.

like image 143
F10 Avatar answered Nov 15 '22 06:11

F10


The recommended methods didn't work for me, while updating GCS from 1.x to 5.x on an older project. Node 14, CommonJs (not modules).

But this finally worked:

const Storage = require('@google-cloud/storage');
const gcs = new Storage({gcsProjectId});
like image 43
Jakay Avatar answered Nov 15 '22 06:11

Jakay