创建行为¶
1. 接口描述¶
接口请求路径:POST /prod-api/acdn/behaviorsRest/createBehaviors
createBehaviors 用于为指定海外 ACDN 分发创建缓存行为(Cache Behavior),配置路径模式、源、协议策略、允许方法及缓存策略等。
Token 获取方式见 密钥鉴权。
2. 输入参数¶
以下请求参数列表仅列出了接口请求参数和部分公共参数,完整公共参数列表见 公共请求参数。
| 参数名称 | 必选 | 类型 | 描述 |
|---|---|---|---|
| distributionId | 是 | Long | 分发主键 ID 示例值: 24 |
| pathPattern | 是 | String | 路径模式 示例值: / |
| targetOriginId | 是 | String | 源或源组 ID 示例值: ztdemo.com |
| cachePolicyId | 是 | String | 缓存策略 ID 示例值: 658327ea-f89d-4fab-a63d-7e88639e58f6 |
| allowedMethods | 是 | Object | 允许的 HTTP 方法 |
| allowedMethods.quantity | 否 | Integer | 允许方法数量 示例值: 2 |
| allowedMethods.items | 否 | Array of String | 允许的方法列表。常用组合:GET/HEAD,或含 OPTIONS/PUT/POST/PATCH/DELETE示例值: ["GET"] |
| allowedMethods.cachedMethods | 否 | Object | 可缓存的 HTTP 方法 |
| allowedMethods.cachedMethods.quantity | 否 | Integer | 可缓存方法数量 示例值: 2 |
| allowedMethods.cachedMethods.items | 否 | Array of String | 可缓存方法列表 示例值: ["GET"] |
| viewerProtocolPolicy | 否 | String | 查看器协议策略,支持以下值:allow-all:HTTP 和 HTTPSredirect-to-https:重定向 HTTP 到 HTTPShttps-only:仅 HTTPS示例值: allow-all |
| compress | 否 | Boolean | 是否自动压缩对象。true 是,false 否示例值: true |
| smoothStreaming | 否 | Boolean | 是否启用 Smooth Streaming。true 是,false 否示例值: true |
| originRequestPolicyId | 否 | String | 源请求策略 ID |
| responseHeadersPolicyId | 否 | String | 响应标头策略 ID |
| realtimeLogConfigArn | 否 | String | 实时日志配置 ARN |
3. 示例¶
示例1 创建缓存行为¶
输入示例¶
{
"pathPattern": "/",
"smoothStreaming": true,
"compress": true,
"originRequestPolicyId": "",
"allowedMethods": {
"quantity": 2,
"items": ["GET"],
"cachedMethods": {
"quantity": 2,
"items": ["GET"]
}
},
"cachePolicyId": "658327ea-f89d-4fab-a63d-7e88639e58f6",
"targetOriginId": "ztdemo.com",
"responseHeadersPolicyId": "",
"viewerProtocolPolicy": "allow-all",
"distributionId": 24
}
代码调用¶
请将 {登录域名}、token 替换为实际值。
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 CreateBehaviorsTest {
public static void main(String[] args) throws Exception {
String url = "https://{登录域名}/prod-api/acdn/behaviorsRest/createBehaviors";
String token = "eyJhbGciOiJIUzUxMiJ9.xxxxxx";
String requestBody = "{"
+ "\"pathPattern\":\"/\","
+ "\"smoothStreaming\":true,"
+ "\"compress\":true,"
+ "\"originRequestPolicyId\":\"\","
+ "\"allowedMethods\":{"
+ "\"quantity\":2,"
+ "\"items\":[\"GET\"],"
+ "\"cachedMethods\":{\"quantity\":2,\"items\":[\"GET\"]}"
+ "},"
+ "\"cachePolicyId\":\"658327ea-f89d-4fab-a63d-7e88639e58f6\","
+ "\"targetOriginId\":\"ztdemo.com\","
+ "\"responseHeadersPolicyId\":\"\","
+ "\"viewerProtocolPolicy\":\"allow-all\","
+ "\"distributionId\":24"
+ "}";
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/acdn/behaviorsRest/createBehaviors"
token := "eyJhbGciOiJIUzUxMiJ9.xxxxxx"
requestBody := []byte(`{
"pathPattern": "/",
"smoothStreaming": true,
"compress": true,
"originRequestPolicyId": "",
"allowedMethods": {
"quantity": 2,
"items": ["GET"],
"cachedMethods": {
"quantity": 2,
"items": ["GET"]
}
},
"cachePolicyId": "658327ea-f89d-4fab-a63d-7e88639e58f6",
"targetOriginId": "ztdemo.com",
"responseHeadersPolicyId": "",
"viewerProtocolPolicy": "allow-all",
"distributionId": 24
}`)
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))
}
输出示例¶
4. 输出参数¶
| 参数名称 | 类型 | 描述 |
|---|---|---|
| code | Integer | 状态码。示例值:200 |
| msg | String | 提示信息。示例值:操作成功 |
5. 错误码¶
| HTTP 状态码 / 业务提示 | 描述 |
|---|---|
| 200 | 成功 |
| 401 | 未鉴权或 Token 无效 |
| 403 | 无权限 |
| id不能为空 | 未传入 distributionId |
6. 数据模型¶
AllowedMethods¶
| 参数名称 | 类型 | 描述 |
|---|---|---|
| quantity | Integer | 允许方法数量 |
| items | Array of String | 允许的 HTTP 方法列表 |
| cachedMethods | Object | 可缓存方法配置 |
| cachedMethods.quantity | Integer | 可缓存方法数量 |
| cachedMethods.items | Array of String | 可缓存的 HTTP 方法列表 |