Cloud Functions REST API:从 zip 文件创建新操作
Cloud Functions REST API: Creating a new action from a zip file
我正在尝试使用以下 curl 从 zip 文件并通过 REST API 创建一个 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?谢谢。
详细模式下的 REST API for creating or updating a Cloud Functions actions is documented in the IBM Cloud Functions API docs. A good way to find out the exact curl / request syntax is to use the IBM Cloud Functions CLI (-v
)。 CLI 只是 REST 的包装器 API 并且在详细模式下会打印所有 REST 详细信息。
以下是可打印内容的相关部分:
Req Body
Body exceeds 1000 bytes and will be truncated
{"namespace":"_","name":"mytest/myaction","exec":{"kind":"nodejs:8","code":"UEsDBBQAAAAIAHJPhEzjlkxc8wYAAH8VAAALABwAX19tYWluX18ucHlVVAkAA+iFxFrohcRadXgLAAEE9AEAAAT0AQAAxVhtb9s2EP6uX8HRCCLBipb002DA69YkbYo17dZ0GwbDMGSKlrXJokfSToNh/313R+rNL2labJiK1iJ578/x7tTBgJ7A/QzYq8IuN3NmdbpYFIIZm9rC2EKYmiIYsB+1ynW6Ykqz1y9u2WWpNhl7uamELVTFrGJClaUUtha2LeQ9S6uMiVJVspYNgnDPWKVhb5lalqU2ZUXFUqZlmaKwtKTNeWpkzKp0JcsHdj
您需要将 binary
字段设置为 true 并将 zip 内容包含为 code
。 curl docs 建议使用@filename 来引用您的 zip 文件:
If you want the contents to be read from a file, use <@filename> as
contents.
您必须 base64 编码 您的 .zip 文件,然后将其作为 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\" ]"
我正在尝试使用以下 curl 从 zip 文件并通过 REST API 创建一个 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?谢谢。
详细模式下的 REST API for creating or updating a Cloud Functions actions is documented in the IBM Cloud Functions API docs. A good way to find out the exact curl / request syntax is to use the IBM Cloud Functions CLI (-v
)。 CLI 只是 REST 的包装器 API 并且在详细模式下会打印所有 REST 详细信息。
以下是可打印内容的相关部分:
Req Body
Body exceeds 1000 bytes and will be truncated
{"namespace":"_","name":"mytest/myaction","exec":{"kind":"nodejs:8","code":"UEsDBBQAAAAIAHJPhEzjlkxc8wYAAH8VAAALABwAX19tYWluX18ucHlVVAkAA+iFxFrohcRadXgLAAEE9AEAAAT0AQAAxVhtb9s2EP6uX8HRCCLBipb002DA69YkbYo17dZ0GwbDMGSKlrXJokfSToNh/313R+rNL2labJiK1iJ578/x7tTBgJ7A/QzYq8IuN3NmdbpYFIIZm9rC2EKYmiIYsB+1ynW6Ykqz1y9u2WWpNhl7uamELVTFrGJClaUUtha2LeQ9S6uMiVJVspYNgnDPWKVhb5lalqU2ZUXFUqZlmaKwtKTNeWpkzKp0JcsHdj
您需要将 binary
字段设置为 true 并将 zip 内容包含为 code
。 curl docs 建议使用@filename 来引用您的 zip 文件:
If you want the contents to be read from a file, use <@filename> as contents.
您必须 base64 编码 您的 .zip 文件,然后将其作为 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\" ]"