3
votes

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.

3

3 Answers

3
votes

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.

0
votes

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.

0
votes
async function getFileData() {
    const bucketName = 'bucket-name';
    const filePath = 'prefix/file-name.csv';
    const file = storage.bucket(bucketName).file(filePath);

    const csvData = await file.download().then(function(contents) {
        console.log("ASCII Contents", contents);
        return contents.toString('ascii');
    }).catch(function(error) {
        console.error("Error File Path:", filePath);
    });
    console.error("CSV File Content:", csvData);
}

getFileData().catch(console.error);