文档首页/ 函数工作流 FunctionGraph/ API参考/ API/ 函数流/ 创建函数流 - CreateWorkflow
更新时间:2026-10-21 GMT+08:00
分享

创建函数流 - CreateWorkflow

功能介绍

创建函数流

调用方法

请参见如何调用API。

授权信息

账号具备所有API的调用权限,如果使用账号下的IAM用户调用当前API,该IAM用户需具备调用API所需的权限,具体权限要求请参见权限和授权项。

URI

POST /v2/{project_id}/fgs/workflows

表1 路径参数

参数

是否必选

参数类型

描述

project_id

是

String

参数解释:

租户项目 ID,获取方式请参见获取项目ID。

约束限制:

不涉及。

取值范围:

只能由英文字母、数字组成,且长度为32个字符

默认取值:

不涉及。

请求参数

表2 请求Body参数

参数

是否必选

参数类型

描述

name

是

String

参数解释:

FunctionGraph工作流名称。

约束限制:

^([_a-zA-Z0-9().-]|[\u4e00-\u9fa5]){1,64}$

取值范围:

[1, 64]

默认取值:

不涉及。

description

否

String

参数解释:

FunctionGraph工作流描述。

约束限制:

^([_a-zA-Z0-9().-]|[\u4e00-\u9fa5]){1,512}$

取值范围:

[1, 512]

默认取值:

不涉及。

start

是

String

参数解释:

FunctionGraph工作流开始节点ID。

约束限制:

不涉及。

取值范围:

[1, 256]

默认取值:

不涉及。

triggers

否

Array of Trigger objects

参数解释:

FunctionGraph工作流触发器列表。

约束限制:

不涉及。

取值范围:

[0, 200]

默认取值:

不涉及。

functions

是

Array of Function objects

参数解释:

FunctionGraph工作流函数列表。

约束限制:

不涉及。

取值范围:

[0, 100]

默认取值:

不涉及。

states

是

Array of OperationState objects

参数解释:

FunctionGraph工作流节点列表。

约束限制:

不涉及。

取值范围:

[1, 100]

默认取值:

不涉及。

constants

是

Object

参数解释:

FunctionGraph工作流常量。

约束限制:

不涉及。

取值范围:

不涉及。

默认取值:

不涉及。

retries

是

Array of Retry objects

参数解释:

FunctionGraph工作流重试策略列表。

约束限制:

不涉及。

取值范围:

[0, 200]

默认取值:

不涉及。

mode

否

String

参数解释:

FunctionGraph工作流模式,默认标准模式。

约束限制:

不涉及。

取值范围:

  • NORMAL:标注模式

  • EXPRESS:快速模式

默认取值:

NORMAL

express_config

否

ExpressConfig object

参数解释:

快速函数流模式配置,当mode为EXPRESS时需要设置。

enterprise_project_id

否

String

参数解释:

FunctionGraph工作流企业项目。

约束限制:

不涉及。

取值范围:

[0, 64]

默认取值:

不涉及。

enable_stream_response

否

Boolean

参数解释:

FunctionGraph工作流是否支持流式返回。

约束限制:

不涉及。

取值范围:

不涉及。

默认取值:

false

表3 Trigger

参数

是否必选

参数类型

描述

trigger_name

是

String

参数解释:

触发器名称

取值范围:

不涉及。

trigger_type

是

String

参数解释

触发器类型

取值范围

  • FLOWTIMER:定时触发器

  • SMN:SMN触发器

  • APIG:APIG触发器(共享版)

  • APIG_DE:APIG触发器(专享版)

  • OBS:OBS触发器

enabled

否

Boolean

参数解释:

是否启用触发器

取值范围:

不涉及。

trigger_config

否

OBSTriggerConfig object

参数解释:

OBS触发器配置

取值范围:

不涉及。

表4 OBSTriggerConfig

参数

是否必选

参数类型

描述

bucket

是

String

参数解释:

OBS桶名

取值范围:

不涉及。

events

是

Array of strings

参数解释:

OBS事件列表

取值范围:

不涉及。

prefix

是

String

参数解释:

OBS对象名前缀

取值范围:

不涉及。

suffix

是

String

参数解释:

OBS对象名后缀

取值范围:

不涉及。

表5 Function

参数

是否必选

参数类型

描述

name

是

String

参数解释:

函数名称,在单个流程中,名称需要唯一

取值范围:

不涉及。

operation

是

String

参数解释:

函数调用URN

取值范围:

不涉及。

metadata

否

Object

参数解释:

函数扩展信息, 由用户自己定制。

取值范围:

不涉及。

表6 OperationState

参数

是否必选

参数类型

描述

action_mode

否

String

参数解释:

节点执行模式

取值范围:

  • sequential: 串行模式

  • parallel: 并行模式

actions

是

Array of Action objects

参数解释:

节点执行操作列表

取值范围:

不涉及。

on_errors

否

Array of OnError objects

参数解释:

节点执行错误处理策略列表

取值范围:

不涉及。

id

是

String

参数解释:

节点ID, 工作流中唯一

取值范围:

不涉及。

name

是

String

参数解释:

节点名称

取值范围:

不涉及。

type

是

String

参数解释:

节点类型表

取值范围:

  • Operation: 操作节点

  • Sleep: 暂停节点

  • End: 结束节点

end

是

Boolean

参数解释:

是否是结束节点

取值范围:

不涉及。

transition

是

String

参数解释:

下一节点ID

取值范围:

不涉及。

state_data_filter

否

StateDataFilter object

参数解释:

过滤表达式

取值范围:

不涉及。

duration

否

Long

参数解释:

时间等待节点等待时间(秒),节点类型为Sleep时为必填,节点类型不为Sleep时无效

取值范围:

不涉及。

表7 Action

参数

是否必选

参数类型

描述

function_ref

否

FunctionRef object

参数解释:

函数调用信息

取值范围:

不涉及。

表8 FunctionRef

参数

是否必选

参数类型

描述

ref_name

是

String

参数解释:

函数引用名称, 需要和外层functions中的name对应

取值范围:

不涉及。

invoke_mode

否

String

参数解释:

函数调用模式, 目前只支持同步调用

取值范围:

  • synchronize: 同步调用

  • asynchronous: 异步调用

arguments

是

Object

参数解释

函数执行时的入参,支持引用constants中的常量

定义方式:参数路径 | 常量值/常量路径

参数路径指输入参数的JsonPath路径,如$.a.b[0].c

常量值可以为数字类型,字符串类型(需要用单引号括起来),布尔类型

常量路径为常量的JsonPath路径,但是根节点需要用$CONST表示,示例:$CONST.a.b

取值范围

不涉及。

表9 OnError

参数

是否必选

参数类型

描述

error

否

String

参数解释:

错误匹配表达式,用来过滤需要处理的异常

取值范围:

不涉及。

transition

否

String

参数解释:

下一步骤节点ID

取值范围:

不涉及。

retry_ref

否

String

参数解释:

重试策略名称

取值范围:

不涉及。

表10 StateDataFilter

参数

是否必选

参数类型

描述

input

否

String

参数解释:

输入过滤表达式(JsonPath)

取值范围:

不涉及。

output

否

String

参数解释:

输出过滤表达式(JsonPath)

取值范围:

不涉及。

表11 Retry

参数

是否必选

参数类型

描述

name

是

String

参数解释:

重试策略名称,在单个流程中,名称需要唯一。

取值范围:

不涉及。

delay

否

Long

参数解释:

重试间隔,单位:秒。若不传,默认为1。

取值范围:

不涉及。

max_attempts

否

Long

参数解释:

最大重试次数,若不传,默认为3。

取值范围:

不涉及。

表12 ExpressConfig

参数

是否必选

参数类型

描述

log_level

否

String

参数解释:

日志级别

取值范围:

  • ALL:全部日志

  • ERROR:错误日志

  • NONE:不记录日志

响应参数

状态码:201

表13 响应Body参数

参数

参数类型

描述

id

String

参数解释:

工作流ID

取值范围:

不涉及。

workflow_urn

String

参数解释:

函数工作流URN,格式为:

urn:fss:<region_id>:<project_id>:workflow:<package>:<workflow_name>:<version>

注意:

package当前只支持default

version当前只支持latest

取值范围:

不涉及。

name

String

参数解释:

工作流名称

取值范围:

不涉及。

description

String

参数解释:

工作流描述

取值范围:

不涉及。

created_time

String

参数解释:

工作流创建时间

取值范围:

格式:yyyy-MM-ddTHH:mm:ssZ,UTC时间

updated_time

String

参数解释:

工作流修改时间

取值范围:

格式:yyyy-MM-ddTHH:mm:ssZ,UTC时间

created_by

String

参数解释:

工作流创建者

取值范围:

不涉及。

enable_stream_response

Boolean

参数解释:

工作流是否支持stream返回

取值范围:

不涉及。

状态码:400

表14 响应Body参数

参数

参数类型

描述

error_code

String

错误码

error_msg

String

错误描述

request_id

String

请求ID

状态码:500

表15 响应Body参数

参数

参数类型

描述

error_code

String

错误码

error_msg

String

错误描述

request_id

String

请求ID

请求示例

创建名为workflow_test,开始节点的名称为test的函数流。

POST /v2/{project_id}/fgs/workflows

{
  "name" : "workflow_test",
  "start" : "test",
  "functions" : [ {
    "name" : "test",
    "operation" : "urn:fss:{region}:c53626012ba84727b938ca8bf03108ef:test:latest"
  } ],
  "constants" : { },
  "states" : [ {
    "id" : "test",
    "name" : "test",
    "type" : "Operation",
    "end" : false,
    "transition" : "end",
    "actions" : [ {
      "function_ref" : {
        "ref_name" : "test",
        "invoke_mode" : "asynchronous",
        "arguments" : {
          "execution_name" : "$.test"
        }
      }
    } ]
  }, {
    "id" : "end",
    "name" : "end",
    "type" : "End",
    "end" : true,
    "transition" : "",
    "state_data_filter" : {
      "input" : "",
      "output" : ""
    },
    "actions" : [ ]
  } ],
  "retries" : [ {
    "name" : "test"
  } ]
}

响应示例

状态码:201

{
  "id" : "48aff955-ea10-4ffd-b426-190bc192dc14",
  "workflow_urn" : "urn:fss:cn-north-7:73d69ae0cfcf460190522d060f05ad:workflow:default:testflow:latest",
  "name" : "test-flow",
  "description" : "desc",
  "created_time" : "2021-01-01T00:00:00Z",
  "updated_time" : "2021-01-01T00:00:00Z",
  "created_by" : "admin"
}

SDK代码示例

SDK代码示例如下。

Java

创建名为workflow_test,开始节点的名称为test的函数流。

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
package com.huaweicloud.sdk.test;

import com.huaweicloud.sdk.core.auth.ICredential;
import com.huaweicloud.sdk.core.auth.BasicCredentials;
import com.huaweicloud.sdk.core.exception.ConnectionException;
import com.huaweicloud.sdk.core.exception.RequestTimeoutException;
import com.huaweicloud.sdk.core.exception.ServiceResponseException;
import com.huaweicloud.sdk.functiongraph.v2.region.FunctionGraphRegion;
import com.huaweicloud.sdk.functiongraph.v2.*;
import com.huaweicloud.sdk.functiongraph.v2.model.*;

import java.util.List;
import java.util.ArrayList;

public class CreateWorkflowSolution {

    public static void main(String[] args) {
        // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
        // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
        String ak = System.getenv("CLOUD_SDK_AK");
        String sk = System.getenv("CLOUD_SDK_SK");
        String projectId = "{project_id}";

        ICredential auth = new BasicCredentials()
                .withProjectId(projectId)
                .withAk(ak)
                .withSk(sk);

        FunctionGraphClient client = FunctionGraphClient.newBuilder()
                .withCredential(auth)
                .withRegion(FunctionGraphRegion.valueOf("<YOUR REGION>"))
                .build();
        CreateWorkflowRequest request = new CreateWorkflowRequest();
        WorkflowCreateBody body = new WorkflowCreateBody();
        List<Retry> listbodyRetries = new ArrayList<>();
        listbodyRetries.add(
            new Retry()
                .withName("test")
        );
        StateDataFilter stateDataFilterStates = new StateDataFilter();
        stateDataFilterStates.withInput("")
            .withOutput("");
        FunctionRef functionRefActions = new FunctionRef();
        functionRefActions.withRefName("test")
            .withInvokeMode(FunctionRef.InvokeModeEnum.fromValue("asynchronous"))
            .withArguments("{\"execution_name\":\"$.test\"}");
        List<Action> listStatesActions = new ArrayList<>();
        listStatesActions.add(
            new Action()
                .withFunctionRef(functionRefActions)
        );
        List<OperationState> listbodyStates = new ArrayList<>();
        listbodyStates.add(
            new OperationState()
                .withActions(listStatesActions)
                .withId("test")
                .withName("test")
                .withType(OperationState.TypeEnum.fromValue("Operation"))
                .withEnd(false)
                .withTransition("end")
        );
        listbodyStates.add(
            new OperationState()
                .withActions()
                .withId("end")
                .withName("end")
                .withType(OperationState.TypeEnum.fromValue("End"))
                .withEnd(true)
                .withTransition("")
                .withStateDataFilter(stateDataFilterStates)
        );
        List<Function> listbodyFunctions = new ArrayList<>();
        listbodyFunctions.add(
            new Function()
                .withName("test")
                .withOperation("urn:fss:{region}:c53626012ba84727b938ca8bf03108ef:test:latest")
        );
        body.withRetries(listbodyRetries);
        body.withConstants(new Object());
        body.withStates(listbodyStates);
        body.withFunctions(listbodyFunctions);
        body.withStart("test");
        body.withName("workflow_test");
        request.withBody(body);
        try {
            CreateWorkflowResponse response = client.createWorkflow(request);
            System.out.println(response.toString());
        } catch (ConnectionException e) {
            e.printStackTrace();
        } catch (RequestTimeoutException e) {
            e.printStackTrace();
        } catch (ServiceResponseException e) {
            e.printStackTrace();
            System.out.println(e.getHttpStatusCode());
            System.out.println(e.getRequestId());
            System.out.println(e.getErrorCode());
            System.out.println(e.getErrorMsg());
        }
    }
}

Python

创建名为workflow_test,开始节点的名称为test的函数流。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# coding: utf-8

import os
from huaweicloudsdkcore.auth.credentials import BasicCredentials
from huaweicloudsdkfunctiongraph.v2.region.functiongraph_region import FunctionGraphRegion
from huaweicloudsdkcore.exceptions import exceptions
from huaweicloudsdkfunctiongraph.v2 import *

if __name__ == "__main__":
    # The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
    # In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
    ak = os.environ["CLOUD_SDK_AK"]
    sk = os.environ["CLOUD_SDK_SK"]
    projectId = "{project_id}"

    credentials = BasicCredentials(ak, sk, projectId)

    client = FunctionGraphClient.new_builder() \
        .with_credentials(credentials) \
        .with_region(FunctionGraphRegion.value_of("<YOUR REGION>")) \
        .build()

    try:
        request = CreateWorkflowRequest()
        listRetriesbody = [
            Retry(
                name="test"
            )
        ]
        stateDataFilterStates = StateDataFilter(
            input="",
            output=""
        )
        functionRefActions = FunctionRef(
            ref_name="test",
            invoke_mode="asynchronous",
            arguments="{\"execution_name\":\"$.test\"}"
        )
        listActionsStates = [
            Action(
                function_ref=functionRefActions
            )
        ]
        listStatesbody = [
            OperationState(
                actions=listActionsStates,
                id="test",
                name="test",
                type="Operation",
                end=False,
                transition="end"
            ),
            OperationState(
                id="end",
                name="end",
                type="End",
                end=True,
                transition="",
                state_data_filter=stateDataFilterStates
            )
        ]
        listFunctionsbody = [
            Function(
                name="test",
                operation="urn:fss:{region}:c53626012ba84727b938ca8bf03108ef:test:latest"
            )
        ]
        request.body = WorkflowCreateBody(
            retries=listRetriesbody,
            constants={},
            states=listStatesbody,
            functions=listFunctionsbody,
            start="test",
            name="workflow_test"
        )
        response = client.create_workflow(request)
        print(response)
    except exceptions.ClientRequestException as e:
        print(e.status_code)
        print(e.request_id)
        print(e.error_code)
        print(e.error_msg)

Go

创建名为workflow_test,开始节点的名称为test的函数流。

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
package main

import (
	"fmt"
	"github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic"
    functiongraph "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/functiongraph/v2"
	"github.com/huaweicloud/huaweicloud-sdk-go-v3/services/functiongraph/v2/model"
    region "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/functiongraph/v2/region"
)

func main() {
    // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
    // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
    ak := os.Getenv("CLOUD_SDK_AK")
    sk := os.Getenv("CLOUD_SDK_SK")
    projectId := "{project_id}"

    auth, err := basic.NewCredentialsBuilder().
        WithAk(ak).
        WithSk(sk).
        WithProjectId(projectId).
        SafeBuild()

    if err != nil {
        fmt.Println(err)
        return
    }

    hcClient, err := functiongraph.FunctionGraphClientBuilder().
         WithRegion(region.ValueOf("<YOUR REGION>")).
         WithCredential(auth).
         SafeBuild()


    if err != nil {
        fmt.Println(err)
        return
    }

    client := functiongraph.NewFunctionGraphClient(hcClient)

    request := &model.CreateWorkflowRequest{}
	var listRetriesbody = []model.Retry{
        {
            Name: "test",
        },
    }
	inputStateDataFilter:= ""
	outputStateDataFilter:= ""
	stateDataFilterStates := &model.StateDataFilter{
		Input: &inputStateDataFilter,
		Output: &outputStateDataFilter,
	}
	invokeModeFunctionRef:= model.GetFunctionRefInvokeModeEnum().ASYNCHRONOUS
	var argumentsFunctionRef interface{} = "{\"execution_name\":\"$.test\"}"
	functionRefActions := &model.FunctionRef{
		RefName: "test",
		InvokeMode: &invokeModeFunctionRef,
		Arguments: &argumentsFunctionRef,
	}
	var listActionsStates = []model.Action{
        {
            FunctionRef: functionRefActions,
        },
    }
	var listStatesbody = []model.OperationState{
        {
            Actions: listActionsStates,
            Id: "test",
            Name: "test",
            Type: model.GetOperationStateTypeEnum().OPERATION,
            End: false,
            Transition: "end",
        },
        {
            Id: "end",
            Name: "end",
            Type: model.GetOperationStateTypeEnum().END,
            End: true,
            Transition: "",
            StateDataFilter: stateDataFilterStates,
        },
    }
	var listFunctionsbody = []model.Function{
        {
            Name: "test",
            Operation: "urn:fss:{region}:c53626012ba84727b938ca8bf03108ef:test:latest",
        },
    }
	var constantsWorkflowCreateBody interface{} = make(map[string]string)
	request.Body = &model.WorkflowCreateBody{
		Retries: listRetriesbody,
		Constants: &constantsWorkflowCreateBody,
		States: listStatesbody,
		Functions: listFunctionsbody,
		Start: "test",
		Name: "workflow_test",
	}
	response, err := client.CreateWorkflow(request)
	if err == nil {
        fmt.Printf("%+v\n", response)
    } else {
        fmt.Println(err)
    }
}

更多

更多编程语言的SDK代码示例,请参见API Explorer的代码示例页签,可生成自动对应的SDK代码示例。

状态码

状态码

描述

201

Created

400

Bad Request

500

Internal Server Error

错误码

请参见错误码。

相关文档