Node.js:Google云存储凭据未在生产环境中加载

时间:2020-10-03 17:21:00

标签: javascript node.js google-cloud-platform google-cloud-storage google-iam

对于我的快速服务器应用程序,我有一个auth.json文件用于身份验证,该文件在开发中效果很好。在我部署的应用程序中,当我上传一个说错误的文件时,身份验证失败:

错误:无法加载默认凭据。浏览至https://cloud.google.com/docs/authentication/getting-started了解更多信息。

为什么我的身份验证在部署后会失败?

当前,我在此项目中的所有存储桶都是“对Internet公开的”,角色allUsers和服务帐户电子邮件都具有完整的Storage Object Creator / Viewer / Admin角色。 auth.json文件与app.js处于同一层次级别。

这是我的app.js代码,其中包含所有库以及上传图像的发布请求。应当注意,uploadThumb()函数每次都起作用,但是uploadFile是导致以下错误的函数。

const express = require("express");
const bodyParser = require("body-parser");
const _ = require("lodash")
const ejs = require("ejs");
const sharp = require('sharp');
const multer = require("multer");
const fs = require("file-system");
const imageThumbnail = require("image-thumbnail");
const {Storage} = require('@google-cloud/storage');
const storage = new Storage({keyFileName: "auth.json"});
const app = express();

//this writes the user submitted image to the local file system
var localStorage = multer.diskStorage({
  destination: function (req, file, cb) {
    const imageDirect = req.body.categories + "/" + req.body.subcat.charAt(0).toUpperCase() + req.body.subcat.slice(1).replace(/ /g, "-") + "/";
    var dir = "./" + imageDirect;
    if(!fs.existsSync(dir)) {
      fs.mkdirSync(dir);
    }
    cb(null, imageDirect)
  },
  filename: function (req, file, cb) {
    let tempImageName = file.originalname;
    req.imageName = tempImageName
    cb(null, tempImageName.replace(/ /g, "-"))
  }
});

var upload = multer({storage: localStorage});

app.post("/upload", upload.single("image"), (req, res) => {

//These define the paths and destinations dynamically
  let cat = req.body.categories;
  let subCatLiteral = req.body.subcat.split(" ");
  let subCat = ""
  for (i=0; i < subCatLiteral.length; i++) {
    var eachWord = subCatLiteral[i].charAt(0).toUpperCase() + subCatLiteral[i].slice(1);
    if (i < subCatLiteral.length-1) {
    subCat = subCat.concat(eachWord + "-");
    } else {
    subCat = subCat.concat(eachWord);
    }
  }
  let img = req.imageName.replace(/ /g, "-");
  let imgTitle = img.split(".")[0];
  let imagePath = cat + '/' + subCat + '/' + img


 //This is uploaded 1st
  async function uploadThumb() { 
    await storage.bucket(thumbs).upload(imgTitle + "-thumb.jpg", {metadata: {cacheControl: "no-cache"}, gzip: true, destination: cat + "/" + subCat + "/" + req.body.name.replace(/ /g, "-") + "-thumb.jpg"})
      uploadFile().catch(console.error);
      fs.unlinkSync(imgTitle + "-thumb.jpg")  //deletes file after upload 
  }


//This is uploaded 2nd, causing  the error
  async function uploadFile() {   
    await storage.bucket(bucketName).upload(imagePath, {metadata: {cacheControl: "no-cache"}, resumable: true, gzip: true, destination: cat + "/" + subCat + "/" + req.body.name.replace(/ /g, "-") + ".jpg"})
      //deletes file after upload
      fs.unlinkSync(`${cat}/${subCat}/${img}`)
      fs.rmdirSync(`./${cat}/${subCat}/`)
      setTimeout(() => {
        fs.rmdirSync(`./${cat}`)
      }, 500)
    await listFiles().catch(console.error);
  }


 //This function starts the chain of uploads
 sharp(imagePath) 
     .resize(null, 300).toFile(imgTitle + "-thumb.jpg", function(err) {
       if (!err) console.log("image resized");
       uploadThumb().catch(console.error);
   })

  res.redirect("/upload#upload");
});

如果有人知道为什么这在生产中会失败,或者我能在文档中的确切位置找到解决方案,我将不胜感激。

这是向passenger.log报告的最新错误

App 21701 output: Error: Could not load the default credentials. Browse to https://cloud.google.com/docs/authentication/getting-started for more information.
App 21701 output:     at GoogleAuth.getApplicationDefaultAsync (/home/kaylakos/nodevenv/rootdir/kayla/12/lib/node_modules/google-auth-library/build/src/auth/googleauth.js:157:19)
App 21701 output:     at processTicksAndRejections (internal/process/task_queues.js:97:5)
App 21701 output:     at async GoogleAuth.getClient (/home/kaylakos/nodevenv/rootdir/kayla/12/lib/node_modules/google-auth-library/build/src/auth/googleauth.js:490:17)
App 21701 output:     at async GoogleAuth.request (/home/kaylakos/nodevenv/rootdir/kayla/12/lib/node_modules/google-auth-library/build/src/auth/googleauth.js:543:24)
App 21701 output:     at async Upload.makeRequest (/home/kaylakos/nodevenv/rootdir/kayla/12/lib/node_modules/gcs-resumable-upload/build/src/index.js:320:21)
App 21701 output:     at async Upload.createURIAsync (/home/kaylakos/nodevenv/rootdir/kayla/12/lib/node_modules/gcs-resumable-upload/build/src/index.js:145:22)

0 个答案:

没有答案