gpt4 book ai didi

ibm-cloud - 云函数REST API : Creating a new action from a zip file

转载 作者:行者123 更新时间:2023-12-03 06:54:53 25 4
gpt4 key购买 nike

我正在尝试使用以下curl通过REST API从zip文件创建nodejs函数:

curl --request PUT --url 'https://my:credentials@openwhisk.eu-gb.bluemix.net/api/v1/namespaces/mynamespace/actions/my_action_name?overwrite=true' --header 'accept: application/json' --header 'content-type: application/json' --data '{"annotations":[{"key":"exec","value":"nodejs:10"},{"key":"web-export","value":true}],"exec":{"kind":"nodejs:10","init":"./action.zip"},"parameters":[{"key":"message","value":"Hello World"}]}'

结果我得到一个错误:

"error":"The request content was malformed:\n'code' must be a string or attachment object defined in 'exec' for 'nodejs:10' actions"

是否可以获得有关如何从 zip 文件并通过 REST API 创建新操作的示例?谢谢。

最佳答案

您必须对 .zip 文件进行 base64 编码,然后将其作为 code 参数传递。我编写了一个 shell 脚本(bash)来编码并创建一个名为“action”的操作。将脚本保存为create.sh并执行脚本./create.sh

#!/bin/sh
ACTION=action
ZIP=$ACTION.zip

base64 $ZIP | echo "\"$(cat)\"" | jq "{namespace:\"_\", name:\"$ACTION\", exec:{kind:\"nodejs:10\", code:., binary:true, main:\"main\"}}" | curl -X PUT -H "Content-Type:application/json" -d @- https://USERNAME:PASSWORD@openwhisk.ng.bluemix.net/api/v1/namespaces/_/actions/$ACTION?overwrite=true

完整代码

app.js 或 index.js 代码

function myAction(args) {
const leftPad = require("left-pad")
const lines = args.lines || [];
return { padded: lines.map(l => leftPad(l, 30, ".")) }
}

exports.main = myAction;

package.json

{
"name": "node",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"left-pad" : "1.1.3"
}
}

运行npm install并压缩文件zip -r action.zip *

测试操作

ibmcloud fn action invoke --result action --param lines "[\"and now\", \"for something completely\", \"different\" ]"

关于ibm-cloud - 云函数REST API : Creating a new action from a zip file,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53794817/

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