3

Is there a way to read and print the data in Cloud Functions from a csv file in Google Cloud Storage?

The Cloud Function API document only says things about downloading the object to your local machine.

I'm creating a fulfillment on dialogflow that will open and read some CSV file on Cloud storage via Cloud functions depending on the query of the user.

mkUltra
  • 2,382
  • 18
  • 38
Renzz
  • 111
  • 2
  • 9

2 Answers2

3

Broadly speaking, you can "download" your cloud storage object to either a NodeJS stream or string, and then parse the CSV from that stream or string. Using a stream is more complicated, but best if your CSV is very large. If your CSV is small and can fit in memory, a string is easier.

To get the data, you can use the Cloud Storage node.js library. In particular, you can use the createReadStream() method or the download() method of the File object to get a stream or a string, respectively.

Once you have that, you can use the CSV Parse API library for node.js.

Prisoner
  • 48,391
  • 6
  • 48
  • 97
  • Do you have to use any awaits to wait on the file stream to complete? – Kehlin Swain Apr 01 '20 at 16:13
  • `createReadStream()` is event based and does not use async/await. `download()` returns a Promise, so you can use await on it. If you have further specific questions, you may want to open up a new question with details and examples of the code that is causing you problems. – Prisoner Apr 01 '20 at 18:59
0

If you're using Java then you can use google-cloud-java's native Java API (NIO) to access files from Google Cloud Storage. See this page for the documentation.

TubesHerder
  • 171
  • 6