Next.js Discord

Discord Forum

Google Cloud Storage with Nextjs

Unanswered
Spruce Grouse posted this in #help-forum
Open in Discord
Spruce GrouseOP
Hi, im facing a problem while trying to autchenticate and get the bucket from google cloud storage.

Here is my code:

import { Storage } from "@google-cloud/storage";

export async function POST() {
  const storage = new Storage({
    projectId: process.env.GCP_STORAGE_PROJECT_ID,
    credentials: {
      client_email: process.env.GCP_STORAGE_CLIENT_EMAIL,
      private_key: process.env.GCP_STORAGE_PRIVATE_KEY,
    },
  });

  const bucketName = "a";
  try {
    const [buckets] = await storage.getBuckets();

    // Check if the specified bucket exists in the list of buckets
    const bucketExists = buckets.some((bucket) => bucket.name === bucketName);

    if (bucketExists) {
      console.log(
        `Successfully authenticated and can access the bucket: ${bucketName}`
      );
      return new Response(JSON.stringify("success"), { status: 200 });
    } else {
      console.log(
        `Authentication successful, but the specified bucket (${bucketName}) does not exist.`
      );
      return new Response(JSON.stringify("success no bucket"), { status: 200 });
    }
  } catch (error) {
    console.error(`Authentication failed or encountered an error: ${error}`);
    return new Response(JSON.stringify("failed"), { status: 200 });
  }
}


and im getting the error:
Authentication failed or encountered an error: Error: error:1E08010C:DECODER routines::unsupported

all of my environment variables are correct tho.
Anyone can help me? 😦

3 Replies

Spruce GrouseOP
anyone have a clue?
1. Do not store service accounts in environment variables.
2. If you do, do not break the service account into pieces. Base64 encode the entire service account, store it in a variable, and then Base64 decode it when required.
3. Your code is failing because the client is being set up with bad credentials. Most likely a corrupted private key.