跳转至

创建分发(新版)

1. 接口描述

接口请求路径:POST /prod-api/acdn/distributions/createDistribution

createDistribution(新版)基于 CloudFront DistributionConfig 创建海外 ACDN 分发。旧版扁平接口见 创建分发(旧版)。账号需具备 acdn:CreateDistribution 权限。

由于 SDK CustomOriginConfig 中 HTTP/HTTPS 端口字段命名不标准,需在请求体根级额外传 httpPorthttpsPort,服务端会写入源自定义配置。

Token 获取方式见 密钥鉴权

2. 输入参数

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

参数名称 必选 类型 描述
oid String 套餐号。示例值:20241234569719224284
httpPort Integer 源 HTTP 端口。默认值:80。示例值:80
httpsPort Integer 源 HTTPS 端口。默认值:443。示例值:443
distributionConfig DistributionConfig CloudFront 分发配置
distributionConfig.callerReference String 唯一调用标识(可用时间戳)
distributionConfig.comment String 备注
distributionConfig.enabled Boolean 是否启用。示例值:true
distributionConfig.priceClass String 价格级别。取值:PriceClass_AllPriceClass_100PriceClass_200。示例值:PriceClass_All
distributionConfig.httpVersion String HTTP 版本。示例值:http2
distributionConfig.isIPV6Enabled Boolean 是否启用 IPv6。示例值:true
distributionConfig.defaultRootObject String 默认根对象
distributionConfig.aliases Object CNAME 集合。quantity + items(String 数组)
distributionConfig.origins Origins 源列表
distributionConfig.defaultCacheBehavior DefaultCacheBehavior 默认缓存行为

3. 输出参数

参数名称 类型 描述
code Integer 状态码。示例值:200
msg String 提示信息。示例值:添加成功
data Object CloudFront Distribution 对象
data.id String 分发 ID。示例值:E1EXAMPLE
data.arn String 分发 ARN
data.status String 部署状态。示例值:InProgress
data.domainName String CloudFront 域名。示例值:dxxxxx.cloudfront.net

4. 示例

示例1 创建分发(新版)

输入示例

{
  "oid": "20241234569719224284",
  "httpPort": 80,
  "httpsPort": 443,
  "distributionConfig": {
    "callerReference": "1773730151743",
    "aliases": {
      "items": [],
      "quantity": 0
    },
    "comment": "",
    "defaultCacheBehavior": {
      "allowedMethods": {
        "items": ["GET", "HEAD"],
        "quantity": 2
      },
      "cachePolicyId": "658327ea-f89d-4fab-a63d-7e88639e58f6",
      "compress": true,
      "originRequestPolicyId": "216adef6-5c7f-47e4-b989-5492eafa07d3",
      "smoothStreaming": false,
      "targetOriginId": "demo.com",
      "viewerProtocolPolicy": "allow-all"
    },
    "defaultRootObject": "",
    "enabled": true,
    "httpVersion": "http2",
    "isIPV6Enabled": true,
    "origins": {
      "items": [
        {
          "connectionAttempts": 3,
          "connectionTimeout": 10,
          "customHeaders": {
            "quantity": 1,
            "items": [
              {
                "headerName": "X-Demo",
                "headerValue": "1"
              }
            ]
          },
          "customOriginConfig": {
            "originKeepaliveTimeout": 5,
            "originProtocolPolicy": "http-only",
            "originReadTimeout": 30,
            "originSslProtocols": {
              "items": ["TLSv1.2"],
              "quantity": 1
            }
          },
          "domainName": "demo.com",
          "id": "demo.com",
          "originPath": "/path",
          "originShield": {
            "enabled": false
          }
        }
      ],
      "quantity": 1
    },
    "priceClass": "PriceClass_All"
  }
}

代码调用

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

curl -X POST 'https://{登录域名}/prod-api/acdn/distributions/createDistribution' \
  -H 'Authorization: Bearer eyJhbGciOiJIUzUxMiJ9.xxxxxx' \
  -H 'Content-Type: application/json' \
  -d '{
  "oid": "20241234569719224284",
  "httpPort": 80,
  "httpsPort": 443,
  "distributionConfig": {
    "callerReference": "1773730151743",
    "enabled": true,
    "priceClass": "PriceClass_All",
    "httpVersion": "http2",
    "isIPV6Enabled": true,
    "origins": {
      "quantity": 1,
      "items": [
        {
          "id": "demo.com",
          "domainName": "demo.com",
          "customOriginConfig": {
            "originProtocolPolicy": "http-only",
            "originSslProtocols": {
              "quantity": 1,
              "items": ["TLSv1.2"]
            }
          }
        }
      ]
    },
    "defaultCacheBehavior": {
      "targetOriginId": "demo.com",
      "viewerProtocolPolicy": "allow-all",
      "cachePolicyId": "658327ea-f89d-4fab-a63d-7e88639e58f6",
      "allowedMethods": {
        "quantity": 2,
        "items": ["GET", "HEAD"]
      },
      "compress": true
    }
  }
}'
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 CreateDistributionNewTest {

    public static void main(String[] args) throws Exception {
        String url = "https://{登录域名}/prod-api/acdn/distributions/createDistribution";
        String token = "eyJhbGciOiJIUzUxMiJ9.xxxxxx";

        String requestBody = "{"
                + "\"oid\":\"20241234569719224284\","
                + "\"httpPort\":80,"
                + "\"httpsPort\":443,"
                + "\"distributionConfig\":{"
                + "\"callerReference\":\"1773730151743\","
                + "\"enabled\":true,"
                + "\"priceClass\":\"PriceClass_All\","
                + "\"httpVersion\":\"http2\","
                + "\"isIPV6Enabled\":true,"
                + "\"origins\":{\"quantity\":1,\"items\":[{"
                + "\"id\":\"demo.com\",\"domainName\":\"demo.com\","
                + "\"customOriginConfig\":{\"originProtocolPolicy\":\"http-only\","
                + "\"originSslProtocols\":{\"quantity\":1,\"items\":[\"TLSv1.2\"]}}"
                + "}]},"
                + "\"defaultCacheBehavior\":{"
                + "\"targetOriginId\":\"demo.com\","
                + "\"viewerProtocolPolicy\":\"allow-all\","
                + "\"cachePolicyId\":\"658327ea-f89d-4fab-a63d-7e88639e58f6\","
                + "\"allowedMethods\":{\"quantity\":2,\"items\":[\"GET\",\"HEAD\"]},"
                + "\"compress\":true"
                + "}"
                + "}"
                + "}";

        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/distributions/createDistribution"
    token := "eyJhbGciOiJIUzUxMiJ9.xxxxxx"

    requestBody := []byte(`{
  "oid": "20241234569719224284",
  "httpPort": 80,
  "httpsPort": 443,
  "distributionConfig": {
    "callerReference": "1773730151743",
    "enabled": true,
    "priceClass": "PriceClass_All",
    "httpVersion": "http2",
    "isIPV6Enabled": true,
    "origins": {
      "quantity": 1,
      "items": [
        {
          "id": "demo.com",
          "domainName": "demo.com",
          "customOriginConfig": {
            "originProtocolPolicy": "http-only",
            "originSslProtocols": {
              "quantity": 1,
              "items": ["TLSv1.2"]
            }
          }
        }
      ]
    },
    "defaultCacheBehavior": {
      "targetOriginId": "demo.com",
      "viewerProtocolPolicy": "allow-all",
      "cachePolicyId": "658327ea-f89d-4fab-a63d-7e88639e58f6",
      "allowedMethods": {
        "quantity": 2,
        "items": ["GET", "HEAD"]
      },
      "compress": true
    }
  }
}`)

    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))
}

输出示例

{
  "code": 200,
  "msg": "添加成功",
  "data": {
    "id": "E1EXAMPLE",
    "arn": "arn:aws:cloudfront::123456789012:distribution/E1EXAMPLE",
    "status": "InProgress",
    "domainName": "dxxxxx.cloudfront.net"
  }
}

5. 错误码

HTTP 状态码 / 业务提示 描述
200 成功
401 未鉴权或 Token 无效
403 无权限
源不能为空! origins.items 为空
源自定义配置不能为空! 缺少 customOriginConfig
套餐不能为空! 未传入 oid
套餐不存在。 oid 不属于当前账号或订单类型不匹配
该套餐可使用分发数量不足 超出套餐分发配额
创建失败 上游创建或落库失败

6. 数据模型

DistributionConfig

CloudFront 分发配置。

参数名称 必选 类型 描述
callerReference String 唯一标识
comment String 备注
enabled Boolean 是否启用
priceClass String 价格级别。取值:PriceClass_AllPriceClass_100PriceClass_200
httpVersion String HTTP 版本
isIPV6Enabled Boolean IPv6
defaultRootObject String 默认根对象
aliases Object quantity + items[](CNAME)
origins Origins 源列表
defaultCacheBehavior DefaultCacheBehavior 默认缓存行为

Origins

源列表。

参数名称 必选 类型 描述
quantity Integer 源数量
items Array of Object 源对象列表
items[].id String 源 ID(需与 targetOriginId 对应)
items[].domainName String 源域名
items[].originPath String 源路径
items[].connectionAttempts Integer 连接次数,范围 1–3。默认值:3
items[].connectionTimeout Integer 连接超时(秒),范围 1–10。默认值:10
items[].customHeaders Object quantity + items[]headerName/headerValue
items[].customOriginConfig Object 自定义源配置
items[].customOriginConfig.originProtocolPolicy String 取值:http-onlyhttps-onlymatch-viewer
items[].customOriginConfig.originReadTimeout Integer 响应超时(秒)。默认值:30
items[].customOriginConfig.originKeepaliveTimeout Integer 保持连接超时(秒)。默认值:5
items[].customOriginConfig.originSslProtocols Object quantity + items[](如 TLSv1.2
items[].originShield Object enabledoriginShieldRegion

DefaultCacheBehavior

默认缓存行为。

参数名称 必选 类型 描述
targetOriginId String 源 ID
viewerProtocolPolicy String 取值:allow-allredirect-to-httpshttps-only
cachePolicyId String 缓存策略 ID
originRequestPolicyId String 源请求策略 ID
responseHeadersPolicyId String 响应头策略 ID
compress Boolean 是否压缩
smoothStreaming Boolean Smooth Streaming
allowedMethods Object quantity + items[],可选 cachedMethods