Google Cloud Storage is an object storage service that lets you store and serve data on Google's infrastructure. This example shows how to use Google Cloud Storage with a Next.js project hosted on Vercel.
Make sure you have installed the Vercel integration as described in the Installation guide. You can install it directly from the integration page.
You'll need the Google Cloud Storage node.js client library (@google-cloud/storage
). First, install it:
npm install @google-cloud/storage
After installing the Google Cloud Platform integration on Vercel, you can access the credentials using our helper function getGCPCredentials
. See the Usage section for more information.
To upload a file to Google Cloud Storage, you need to create a bucket. Create a bucket using the Google Cloud Console or another method. You can then use the following code to upload JSON data to the bucket:
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', });
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); const [data] = await file.download(); console.log(JSON.parse(data.toString()));
Static Asset Hosting: Google Cloud Storage can serve as a reliable and scalable platform to host static assets for your Vercel applications. You can store images, videos, CSS files, JavaScript libraries, and other assets on Google Cloud Storage buckets. This helps offload the serving of these assets from your Vercel server, resulting in faster load times for your users.
Backup and Data Archiving: Google Cloud Storage offers secure and cost-effective storage options, making it an excellent choice for backing up and archiving data from your Vercel applications. You can store application backups, logs, and historical data in Google Cloud Storage buckets, ensuring data durability and easy retrieval when needed.
User-generated Content: If your Vercel application allows users to upload content, such as images, documents, or media files, you can use Google Cloud Storage to store and manage this user-generated content. This approach ensures that your application's server remains focused on handling user interactions and logic, while the storage of uploaded files is efficiently managed by Google Cloud Storage.
Integration with Data Processing Pipelines: If your Vercel application involves data processing, ETL (Extract, Transform, Load) pipelines, or data analysis, Google Cloud Storage can act as a landing zone for your data. You can store raw or processed data in Google Cloud Storage buckets, allowing other services in the Google Cloud ecosystem, such as BigQuery or Dataflow, to efficiently process and analyze this data.