将文件保存到 GCS 在 Next.js 生产版本中不起作用

问题描述 投票:0回答:1

这些是我正在使用的模块。

"@google-cloud/storage": "^7.7.0"
"next": "14.1.0"

这是代码。

"use server";

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

const storage = new Storage({
  keyFilename: process.env.GOOGLE_APPLICATION_CREDENTIALS,
});
const bucket = storage.bucket(process.env.GCP_BUCKET_NAME || '');

export async function saveToCloud() {
  const readable = fs.createReadStream("sample.png");
  const blob = bucket.file("sample_on_cloud.png");
  const blobStream = blob.createWriteStream();

  try {
    await new Promise(
      (resolve, reject) => {
        readable.pipe(blobStream)
          .on('error', reject)
          .on('finish', resolve)
      }
    )
  }
  catch(e) {
    console.log('getting error: ', e);  // in production build, I am getting this error.
    return {error: true}
  }
  
  return {success: true}
}

在开发环境下,完美运行。 所有环境变量都正常工作。 但是当涉及到生产版本时,它给了我错误。

如何解决这个问题?

typescript stream upload google-cloud-storage next.js14
1个回答
0
投票

看来是next.js的问题

将以下内容添加到您的 next.config.mjs 下一个配置属性应该会有所帮助

experimental:{
  serverMinification: false
},
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.