新增或修改函数¶
1. 接口描述¶
接口请求路径:POST /prod-api/multiCdn/function/modifyCdnFunction
modifyCdnFunction 用于新增或修改 CDN 函数。不传 functionGrnId 时为新增函数;传入 functionGrnId 时为修改已有函数。修改成功后会对关联域名生成配置下发任务。账号需具备 cdn2:EditFunction 权限。
Token 获取方式见 密钥鉴权。
注意
domainGrnIds 为**全量替换**:传入时会先解除该函数已有的全部域名关联,再按本次传入的列表重新绑定;不传或传 null 时会解除该函数的全部域名关联。请确保传入完整的关联域名列表。
2. 输入参数¶
以下请求参数列表仅列出了接口请求参数和部分公共参数,完整公共参数列表见 公共请求参数。
| 参数名称 | 必选 | 类型 | 描述 |
|---|---|---|---|
| name | 是 | String | 函数名称。示例值:图片缓存规则 |
| functionName | 是 | String | 函数类别。取值:cache(缓存配置)。示例值:cache |
| config | 是 | Object | 函数配置,字段随函数类别不同而不同。缓存配置字段见下方说明 |
| functionGrnId | 否 | String | 函数唯一 ID。不传为新增函数,传入为修改该函数。示例值:grn:test:multi-cdn:001:function/0a1b2c3d4e5f6789 |
| remark | 否 | String | 备注。示例值:静态图片缓存 1 天 |
| domainGrnIds | 否 | Array of String | 要绑定的域名唯一 ID 列表,全量替换。示例值:["grn:test:multi-cdn:001:domain/01234565d940e553de8e"] |
config(functionName 为 cache 时)字段说明:
| 参数名称 | 必选 | 类型 | 描述 |
|---|---|---|---|
| contentType | 是 | String | 缓存内容类型。取值:directory(目录)、file(文件后缀)、path(全路径)。示例值:file |
| contentValue | 是 | String | 缓存内容,多个值用英文逗号分隔。示例值:jpg,png,gif |
| cacheTime | 是 | Long | 缓存时间,单位秒。示例值:86400 |
| ignoreParams | 是 | Boolean | 是否忽略 URL 参数。示例值:false |
3. 输出参数¶
| 参数名称 | 类型 | 描述 |
|---|---|---|
| code | Integer | 状态码。示例值:200 |
| msg | String | 提示信息。示例值:更新成功 |
| data | Object | 函数信息 |
| data.functionId | String | 函数内部 ID。示例值:1024 |
4. 示例¶
输入示例¶
{
"functionName": "cache",
"name": "图片缓存规则",
"remark": "静态图片缓存 1 天",
"config": {
"contentType": "file",
"contentValue": "jpg,png,gif",
"cacheTime": 86400,
"ignoreParams": false
},
"domainGrnIds": ["grn:test:multi-cdn:001:domain/01234565d940e553de8e"]
}
代码调用¶
请将 {登录域名}、token 替换为实际值。
curl -X POST 'https://{登录域名}/prod-api/multiCdn/function/modifyCdnFunction' \
-H 'Authorization: Bearer eyJhbGciOiJIUzUxMiJ9.xxxxxx' \
-H 'Content-Type: application/json' \
-d '{
"functionName": "cache",
"name": "图片缓存规则",
"remark": "静态图片缓存 1 天",
"config": {
"contentType": "file",
"contentValue": "jpg,png,gif",
"cacheTime": 86400,
"ignoreParams": false
},
"domainGrnIds": ["grn:test:multi-cdn:001:domain/01234565d940e553de8e"]
}'
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
public class ModifyCdnFunctionTest {
public static void main(String[] args) throws Exception {
String url = "https://{登录域名}/prod-api/multiCdn/function/modifyCdnFunction";
String token = "eyJhbGciOiJIUzUxMiJ9.xxxxxx";
String requestBody = "{"
+ "\"functionName\":\"cache\","
+ "\"name\":\"图片缓存规则\","
+ "\"remark\":\"静态图片缓存 1 天\","
+ "\"config\":{"
+ " \"contentType\":\"file\","
+ " \"contentValue\":\"jpg,png,gif\","
+ " \"cacheTime\":86400,"
+ " \"ignoreParams\":false"
+ "},"
+ "\"domainGrnIds\":[\"grn:test:multi-cdn:001:domain/01234565d940e553de8e\"]"
+ "}";
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("Authorization", "Bearer " + token);
httpPost.setEntity(new StringEntity(requestBody, ContentType.APPLICATION_JSON));
try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
int statusCode = response.getStatusLine().getStatusCode();
String body = EntityUtils.toString(response.getEntity(), "UTF-8");
System.out.println("Status Code: " + statusCode);
System.out.println("Body: " + body);
}
}
}
}
package main
import (
"bytes"
"fmt"
"io"
"net/http"
)
func main() {
url := "https://{登录域名}/prod-api/multiCdn/function/modifyCdnFunction"
token := "eyJhbGciOiJIUzUxMiJ9.xxxxxx"
requestBody := []byte(`{
"functionName": "cache",
"name": "图片缓存规则",
"remark": "静态图片缓存 1 天",
"config": {
"contentType": "file",
"contentValue": "jpg,png,gif",
"cacheTime": 86400,
"ignoreParams": false
},
"domainGrnIds": ["grn:test:multi-cdn:001:domain/01234565d940e553de8e"]
}`)
req, err := http.NewRequest(http.MethodPost, url, bytes.NewReader(requestBody))
if err != nil {
panic(err)
}
req.Header.Set("Authorization", "Bearer "+token)
req.Header.Set("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
panic(err)
}
fmt.Println("Status Code:", resp.StatusCode)
fmt.Println("Body:", string(body))
}
输出示例¶
5. 错误码¶
| HTTP 状态码 / 业务提示 | 描述 |
|---|---|
| 200 | 成功 |
| 401 | 未鉴权或 Token 无效 |
| 403 | 无权限 |
| 函数名称不能为空 | 未传 name |
| 更新CDN函数失败:函数配置不能为空 | 未传 config |
| 更新CDN函数失败:请确认函数ID是否正确 | functionGrnId 对应的函数不存在或不属于当前账号 |
| 更新CDN函数失败:该CDN函数类别无效 | functionName 不是 cache |
| 更新CDN函数失败:该函数所关联的域名无编辑权限,请确认您对该域名拥有管理权限后重试。 | domainGrnIds 中存在无管理权限的域名 |
| update fail: ignoreParams is not null | config.ignoreParams 未传 |