gpt4 book ai didi

node.js - 为什么在使用 zip 或直接使用编辑器在 gcloud 中部署 nodejs 函数时遇到错误?

转载 作者:行者123 更新时间:2023-12-05 04:39:50 26 4
gpt4 key购买 nike

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

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

(Build failed: function.js does not exist; Error 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

最佳答案

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

关于node.js - 为什么在使用 zip 或直接使用编辑器在 gcloud 中部署 nodejs 函数时遇到错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70390037/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com