Just wanting to add an answer to recommend the Google Cloud Platform community integration for vercel that made this so easy for me:
https://www.gcpvercel.com/
You simply install the integration on your vercel deployment:
https://vercel.com/integrations/gcp/new
The UI then takes you through uploading your Google Application Credentials JSON file to vercel, and the creds are automatically available as environment variables on your deployment(s) without having to manually add them.
Copy the kickoff code provided to auth your application and it works from there.
export const getGCPCredentials = () => {
// for Vercel, use environment variables
return process.env.GCP_PRIVATE_KEY
? {
credentials: {
client_email: process.env.GCP_SERVICE_ACCOUNT_EMAIL,
private_key: process.env.GCP_PRIVATE_KEY,
},
projectId: process.env.GCP_PROJECT_ID,
}
// for local development, use gcloud CLI
: {};
};
Example
import { Storage } from '@google-cloud/storage';
export const storageClient = new Storage(getGCPCredentials());
const bucketName = 'my-bucket';
const fileName = 'my-file.json';
const file = storageClient.bucket(bucketName).file(fileName);
await file.save(JSON.stringify({
foo: 'bar',
}), {
contentType: 'application/json',
});
Firestore example
import { Firestore } from '@google-cloud/firestore';
const firestore = new Firestore(getGCPCredentials());
const document = firestore.doc('posts/intro-to-firestore');
await document.set({
title: 'Welcome to Firestore',
body: 'Hello World',
});
console.log('Entered new data into the document');