【问题标题】:Why am I encountering an error when deploying a nodejs function in gcloud with a zip or directly with editor?为什么我在使用 zip 或直接使用编辑器在 gcloud 中部署 nodejs 函数时遇到错误?
【发布时间】:2022-01-20 04:39:57
【问题描述】:

我想实现云功能,我在vscode上做。我想我用了所有必要的东西来实现云功能。

为了测试这个,我安装了@google-cloud/storage,它在我的机器上运行良好,但是当我压缩 zip 以在部署时将其导入 GCP 时,它给了我一个错误:

(构建失败:function.js 不存在;错误 ID:7485c5b6)

虽然我在 GCP 的入口点中明确指出了我的 exports.backup 函数的入口点。

这是我正在尝试运行的代码 - 一定缺少某些东西,但我无法弄清楚。

package.json:

{
  "name": "export-mysql",
  "version": "1.0.0",
  "description": "create backup database production",
  "main": "index.js",
  "scripts": {
    "backup": "functions-framework --target=backup"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "chalk": "^4.1.2",
    "dayjs": "^1.10.7",
    "dotenv": "^10.0.0",
    "googleapis": "^92.0.0",
    "@google-cloud/functions-framework": "^2.1.0"
  }
}

代码:

const { google } = require("googleapis");
const sqlAdmin = google.sqladmin("v1beta4");
const dayjs = require("dayjs");
const chalk = require("chalk");
const dotenv = require("dotenv");
const log = console.log;
const error = console.error;

dotenv.config({ path: "./config/.env" });
let = respo = "";
authorize(function (authClient) {
    const date = dayjs(Date.now()).format("YYYYMMDDHHmm");

    var request = {
        project: "project",
        instance: "database-prod",
        resource: {
            exportContext: {
                databases: ["database"],
                fileType: "SQL",
                kind: "sql#exportContext",
                uri: `gs://backup-database-pop/backup-prod-${date}.gz`,
            },
        },
        auth: authClient,
    };

    sqlAdmin.instances.export(request, function (err, response) {
        if (err) {
            error(chalk.yellow.bold(`Status: ${err.code}`));
            log(chalk.red.bold(`Message: ${err.message}`));

            return;
        }
        // TODO: Change code below to process the `response` object:
        // log(chalk.yellow.bold(`Status: ${response.status}`));
        log(chalk.greenBright.bold(`Database Exporter dans le bucket -> backup-database-pop fichier: backup-prod-${date}.sql`));
        respo = `Database Exporter dans le bucket -> backup-database-pop fichier: backup-prod-${date}.sql`;
        return respo;
        // log.log(JSON.stringify(response, null, 2));
    });
});
function authorize(callback) {
    google.auth
        .getClient({
            scopes: ["https://www.googleapis.com/auth/cloud-platform"],
        })
        .then((client) => {
            callback(client);
        })
        .catch((err) => {
            error(chalk.red.bold("authentication failed: ", err));
        });
}
exports.backup = (req, res) => {
    res.end();
    log(respo);
    log("Function complete!");
};

这是压缩文件夹的结构:

functionFolder
 folder ->  config/.env
 index.js
 package.json
 package-lock.json
 authorize.json

【问题讨论】:

  • 你如何构建你的 zip?你能分享一下它的内部结构吗?
  • @guillaumeblaquiere 我只是把文件夹结构我压缩了
  • 你不能在 zip 的层次结构的根目录下有文件夹,至少对于你的索引和你的 package.json 文件。您可以拥有其他资源的文件夹。
  • @guillaumeblaquiere 谢谢我不明白实际上不能有根文件夹
  • @lidianmanoha 如果您找到了解决方案,您能否将其发布为答案并接受它,以便它也可以帮助其他社区成员?

标签: node.js google-cloud-functions gcloud


【解决方案1】:

这是您必须选择文件并压缩它们而不是压缩文件夹的解决方案

【讨论】:

  • 确实,所以我的问题;)
  • @guillaumeblaquiere 谢谢你的帮助
猜你喜欢
  • 2019-09-19
  • 2021-08-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-10
  • 1970-01-01
  • 2019-06-10
  • 2011-06-01
相关资源
最近更新 更多