Next.js Discord

Discord Forum

Next13 API ... GEt image from GCP bucket

Unanswered
Giant panda posted this in #help-forum
Open in Discord
Giant pandaOP
Trying to create a call to a nextjs 13 API that retrieves an image from a GCP bucket. Current code. Something is NQR.

import { NextApiRequest } from "next";
import { NextRequest, NextResponse } from "next/server";
import { Storage } from '@google-cloud/storage';
import { string } from "zod";

export async function GET(request : Request, resp: Response) {
    console.log('Trying to get image in api')
    const storage = new Storage({
      //const storage = {
        projectId: process.env.PROJECT_ID,
        credentials: {
          client_email: process.env.CLIENT_EMAIL,
          private_key: process.env.PRIVATE_KEY?.replace(/\\n/g, '\n'),
        },
      });

    console.log('storage:', storage)
    
    const bucketName = process.env.GCP_BUCKET_NAME
    const fileName = 'ARP-logo-RGB_red-300x120.png'
    const contents = await storage.bucket(bucketName).file(fileName);


    console.log(typeof(contents))
    console.log(typeof(Object.keys(contents)))
    

    return new NextResponse({
      // headers: {
      //   'Content-Type': 'application/json', 
      // },
      body: contents.createReadStream()
    })

7 Replies

Double check your const contents = ….
I think that’s supposed to be two promises
So two awaits
Store.bucket is a promise, and bucket.file is a promise
(I think)
I’ve never used these APIs
Just reading your code and making guesses