1

I need to access a file stored on Google Cloud Storage, this is not a file that is uploaded, instead it is created by a cloud function. I cannot figure out how to access this file using Node.

I have tried many of the things recommended on Stackoverflow.

Similarly, I have checked out the sample Google project but that only uses read streams and because I am not uploading the file, I couldn't figure out how to use that.

The closest I have gotten is modifing the first link to get this

var {Storage} = require('@google-cloud/storage');
var gcs = new Storage({
  keyFilename: path.join("_file.json"),
  projectId: 'ProjectId'
});

const chosenBucket = gcs.bucket("bucket_name");
var file = chosenBucket('file.json');

This causes a TypeError: TypeError: bucket is not a function

How can I access and read the json located within the file?

alvst
  • 95
  • 1
  • 13
  • Do you know the file name? Do you want to launch this process when the file is write? What's your version of Node? – guillaume blaquiere Sep 16 '19 at 21:13
  • Yes I know the file name. I do not need to write to the file at all, just read from it, but it is a file that will be newly created for each instance. It is version 12 of node. – alvst Sep 16 '19 at 21:36
  • Also, the permissions on the file are public. – alvst Sep 16 '19 at 21:36

1 Answers1

2
const chosenBucket = gcs.bucket("bucket_name");
var file = chosenBucket('file.json');

chosenBucket is not a function. It's a Bucket object. Do something like this:

const chosenBucket = gcs.bucket("bucket_name");
var file = chosenBucket.file('file.json');

const download_options = {
  // The path to which the file should be downloaded, e.g. "./file.txt"
  destination: destFilename,
};
await file.download(download_options)

See an example: https://github.com/googleapis/nodejs-storage/blob/master/samples/files.js

Brandon Yarbrough
  • 37,021
  • 23
  • 116
  • 145