GCP Cloud Function Updates for Pi Cal
Feb 4, 2023

Previously I had been updating the Cloud Function with a new token.json and deploying it as a new version. I've gone through and updated this to fetch from a bucket that's not accessible from the internet. After handling the auth, it should now grab the file from the bucket and send it instead. Here are my additions:

At the top before the exports:

const {Storage} = require('@google-cloud/storage');

After the auth:

// Initialize the storage client
const storage = new Storage();

// Set the bucket name and file name
const bucketName = 'bucket-name';
const fileName = 'token.json';

// Read the file contents
storage
    .bucket(bucketName)
    .file(fileName)
    .createReadStream()
    .on('error', err => {
        console.error(err);
        res.status(500).send('Error reading file');
    })
    .on('data', data => {
        res.write(data);
    })
    .on('end', () => {
        res.end();
    });

Testing with Postman and everything works without needing to change source on the remote Raspberry Pi Zero W.