跳转至

创建行为

1. 接口描述

接口请求路径:POST /prod-api/acdn/behaviorsRest/createBehaviors

createBehaviors 用于为指定海外 ACDN 分发创建缓存行为(Cache Behavior),配置路径模式、源、协议策略、允许方法及缓存策略等。账号需具备 acdn:EditDistribution 权限。

Token 获取方式见 密钥鉴权

2. 输入参数

以下请求参数列表仅列出了接口请求参数和部分公共参数,完整公共参数列表见 公共请求参数

参数名称 必选 类型 描述
distributionId Long 分发主键 ID。示例值:24
pathPattern String 路径模式。示例值:/images/*
targetOriginId String 源或源组 ID。示例值:origin.example.com
cachePolicyId String 缓存策略 ID。示例值:658327ea-f89d-4fab-a63d-7e88639e58f6
allowedMethods AllowedMethods 允许的 HTTP 方法
viewerProtocolPolicy String 查看器协议策略。取值:allow-all(HTTP 和 HTTPS)、redirect-to-https(重定向 HTTP 到 HTTPS)、https-only(仅 HTTPS)。示例值:allow-all
compress Boolean 是否自动压缩对象。示例值:true
smoothStreaming Boolean 是否启用 Smooth Streaming。示例值:false
originRequestPolicyId String 源请求策略 ID
responseHeadersPolicyId String 响应标头策略 ID
realtimeLogConfigArn String 实时日志配置 ARN

3. 输出参数

参数名称 类型 描述
code Integer 状态码。示例值:200
msg String 提示信息。示例值:操作成功
data Boolean 是否创建成功。示例值:true

4. 示例

示例1 创建缓存行为

输入示例

{
  "pathPattern": "/images/*",
  "smoothStreaming": false,
  "compress": true,
  "originRequestPolicyId": "",
  "allowedMethods": {
    "quantity": 2,
    "items": ["GET", "HEAD"],
    "cachedMethods": {
      "quantity": 2,
      "items": ["GET", "HEAD"]
    }
  },
  "cachePolicyId": "658327ea-f89d-4fab-a63d-7e88639e58f6",
  "targetOriginId": "origin.example.com",
  "responseHeadersPolicyId": "",
  "viewerProtocolPolicy": "allow-all",
  "distributionId": 24
}

代码调用

请将 {登录域名}token 替换为实际值。

curl -X POST 'https://{登录域名}/prod-api/acdn/behaviorsRest/createBehaviors' \
  -H 'Authorization: Bearer eyJhbGciOiJIUzUxMiJ9.xxxxxx' \
  -H 'Content-Type: application/json' \
  -d '{
  "pathPattern": "/images/*",
  "smoothStreaming": false,
  "compress": true,
  "originRequestPolicyId": "",
  "allowedMethods": {
    "quantity": 2,
    "items": ["GET", "HEAD"],
    "cachedMethods": {
      "quantity": 2,
      "items": ["GET", "HEAD"]
    }
  },
  "cachePolicyId": "658327ea-f89d-4fab-a63d-7e88639e58f6",
  "targetOriginId": "origin.example.com",
  "responseHeadersPolicyId": "",
  "viewerProtocolPolicy": "allow-all",
  "distributionId": 24
}'
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\":\"/images/*\","
                + "\"smoothStreaming\":false,"
                + "\"compress\":true,"
                + "\"originRequestPolicyId\":\"\","
                + "\"allowedMethods\":{"
                + "\"quantity\":2,"
                + "\"items\":[\"GET\",\"HEAD\"],"
                + "\"cachedMethods\":{\"quantity\":2,\"items\":[\"GET\",\"HEAD\"]}"
                + "},"
                + "\"cachePolicyId\":\"658327ea-f89d-4fab-a63d-7e88639e58f6\","
                + "\"targetOriginId\":\"origin.example.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": "/images/*",
  "smoothStreaming": false,
  "compress": true,
  "originRequestPolicyId": "",
  "allowedMethods": {
    "quantity": 2,
    "items": ["GET", "HEAD"],
    "cachedMethods": {
      "quantity": 2,
      "items": ["GET", "HEAD"]
    }
  },
  "cachePolicyId": "658327ea-f89d-4fab-a63d-7e88639e58f6",
  "targetOriginId": "origin.example.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))
}

输出示例

{
  "msg": "操作成功",
  "code": 200,
  "data": true
}

5. 错误码

HTTP 状态码 / 业务提示 描述
200 成功
401 未鉴权或 Token 无效
403 无权限
id不能为空 未传入 distributionId

6. 数据模型

AllowedMethods

允许与可缓存的 HTTP 方法。

参数名称 必选 类型 描述
quantity Integer 允许方法数量。示例值:2
items Array of String 允许的方法列表,常用:GET/HEAD,或含 OPTIONS/PUT/POST/PATCH/DELETE。示例值:["GET", "HEAD"]
cachedMethods CachedMethods 可缓存的 HTTP 方法

CachedMethods

可缓存方法。

参数名称 必选 类型 描述
quantity Integer 可缓存方法数量。示例值:2
items Array of String 可缓存方法列表。示例值:["GET", "HEAD"]