簡體   English   中英

在 Blitz.js api 處理程序中運行時,Google Cloud Storage 調用失敗並顯示“error:0909006C:PEM routines:get_name:no start line”

[英]Google Cloud Storage call fails with "error:0909006C:PEM routines:get_name:no start line" when run in Blitz.js api handler

當我嘗試在Blitz.js /api 處理程序中使用@google-cloud/storage時,它會生成此錯誤:

error:0909006C:PEM routines:get_name:no start line
    at Sign.sign (internal/crypto/sig.js:110:29)
    at NodeCrypto.sign (C:\Users\markj\workspace\myapp\node_modules\google-auth-library\build\src\crypto\node\crypto.js:35:23)
    at GoogleAuth.sign (C:\Users\markj\workspace\myapp\node_modules\google-auth-library\build\src\auth\googleauth.js:561:39)
    at runMicrotasks (<anonymous>)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
    at async sign (C:\Users\markj\workspace\myapp\node_modules\@google-cloud\storage\build\src\signer.js:174:35) {
  name: 'SigningError'

但是,當我使用node test_api.js在本地運行它時,它工作正常......

這是我的代碼:

// test_api.js

const {Storage} = require('@google-cloud/storage');

const client_id = process.env.GCP_STORAGE_ADMIN_CLIENT_ID
const projectId = process.env.GCP_PROJECT_ID
const client_email = process.env.GCP_STORAGE_ADMIN_CLIENT_EMAIL
const private_key = process.env.GCP_STORAGE_ADMIN_PRIVATE_KEY

const storage = new Storage({
    projectId,
    credentials: {
        client_id,
        client_email,
        private_key,
    }
});

async function listBuckets() {
    console.log('PRIVATE KEY: ', private_key) // the error seems to indicate there is an issue here
    // Output: "-----BEGIN PRIVATE KEY-----\n[the private key]\n-----END PRIVATE KEY-----\n"
    const [buckets] = await storage.getBuckets();
    console.log('Buckets:');
    buckets.forEach(bucket => {
      console.log(bucket.name);
    });
}

module.exports = {
    listBuckets // this function errors when called within Next.js /api handler
}

// When I uncomment this and run the file directly with node, it works
// listBuckets()

該錯誤似乎表明我的私鑰的開始/前綴有問題,但我從 Google 服務帳戶 JSON 文件中准確復制了它。 它看起來像這樣:

-----BEGIN PRIVATE KEY-----\n[the private key]\n-----END PRIVATE KEY-----\n

原來是我的.env.local環境變量中錯位的尾隨逗號。

GCP_STORAGE_ADMIN_PRIVATE_KEY="[the private key]",

尾隨的逗號不明顯,因為我關閉了自動換行,並且 console.logs 沒有顯示逗號,但它以某種方式讀取了逗號並導致整個私鑰被錯誤地解析。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM