akamai cdn api 使用

Akamai CDN API 说明文档,调用 API 之前需要先根据官方文档说明 Create API Client

Python SDK 使用

根据官方文档说明安装 edgegrid-python,并配置 Create API Client Key 信息 [1]

pip install edgegrid-python

Python SDK 使用官方示例

Akamai API 列表

获取账号信息

大多数 API 使用都需要提供 groupIdaccountIdcontractIds,通过以下接口获取这些信息 [2]

python
>>> import requests
>>> from akamai.edgegrid import EdgeGridAuth, EdgeRc
>>> from urllib.parse import urljoin

>>> edgerc = EdgeRc('~/.edgerc')
>>> section = 'default'
>>> baseurl = 'https://%s' % edgerc.get(section, 'host')

>>> s = requests.Session()
>>> s.auth = EdgeGridAuth.from_edgerc(edgerc, section)

>>> result = s.get(urljoin(baseurl, '/papi/v1/groups'))

>>> result.json()
{'accountId': 'act_F-AC-488298736', 'accountName': 'Guangzhou kl Network Technology Co., Ltd', 'groups': {'items': [{'groupName': 'Guangzhou extreme-G-8HGkt-DSA', 'groupId': 'grp_186987767', 'contractIds': ['ctr_G-3JDoujhh']}]}}

如果 Property 中存在分组,每个分组都有一个独立的 groupId

获取 CPCode

通过以下接口及参数获取 CPCode [3]

python
>>> import requests
>>> from akamai.edgegrid import EdgeGridAuth, EdgeRc
>>> from urllib.parse import urljoin

>>> edgerc = EdgeRc('~/.edgerc')
>>> section = 'default'
>>> baseurl = 'https://%s' % edgerc.get(section, 'host')

>>> s = requests.Session()
>>> s.auth = EdgeGridAuth.from_edgerc(edgerc, section)

>>> result = s.get(urljoin(baseurl, '/papi/v1/cpcodes'), params={'contractId': 'ctr_G-ctr_G-3JDoujhh', 'groupId': 'grp_186987767'}, headers={'PAPI-Use-Prefixes': 'True'})

获取指定 CPCode 的流量信息

此处主要演示获取指标 edgeBytes 的流量,具体 API 使用方式可参考官方文档 [4]

查询所有 CPCode 的流量

python
>>> import requests
>>> from akamai.edgegrid import EdgeGridAuth, EdgeRc
>>> from urllib.parse import urljoin

>>> edgerc = EdgeRc('~/.edgerc')
>>> section = 'default'
>>> baseurl = 'https://%s' % edgerc.get(section, 'host')

>>> s = requests.Session()
>>> s.auth = EdgeGridAuth.from_edgerc(edgerc, section)

>>> params = {'start': '2022-12-31T00:00:00Z', 'end': '2023-01-01T00:00:00Z', 'allObjectIds': True, 'metrics': 'edgeBytes'}

>>> result = s.get(urljoin(baseurl, '/reporting-api/v1/reports/bytes-by-cpcode/versions/1/report-data'), params=params)

获取指定 CPCode 的流量信息

python
>>> s = requests.Session()
>>> s.auth = EdgeGridAuth.from_edgerc(edgerc, section)

>>> params = {'start': '2022-12-01T00:00:00Z', 'end': '2023-01-01T00:00:00Z', 'objectIds': '1335489', 'metrics': 'edgeBytes'}

>>> result = s.get(urljoin(baseurl, '/reporting-api/v1/reports/bytes-by-cpcode/versions/1/report-data'), params=params)

获取 property

要获取 Property 信息,参考代码 [5]

要获取指定 Property 信息,参考代码 [7]

获取 Property 的 rule 信息

参考文档 [6]

获取证书信息

API 参考文档

  • 需要传入 contractId 参数(注意事项: 获取 contractId 返回的 contractId 类似 ctr_G-2PLTYIP,在这里要改成 G-2PLTYIP,即去除前面的 ctr_ 部分,否则会报 type=Forbidden, title=Invalid Contract, detail=The current contract does not belong to ACG list., source=Contract ID: CTR_G-2PLTYIP 错误
  • 以及指定 header。否则会报错: {'type': 'https://akab-lnvqfow3zngi6lr7-3l3nwwj2hnx5xrk5.luna.akamaiapis.net/cps/v2/error-types/missing-accept-header', 'title': 'A valid HTTP Accept header is required.', 'instance': 'https://akab-lnvqfow3zngi6lr7-3l3nwwj2hnx5xrk5.luna.akamaiapis.net/cps/v2/error-types/missing-accept-header?id=039e80fc61084409b4ab2fcf572cc9ce'}
    result = s.get(urljoin(baseurl, '/cps/v2/enrollments'), 
    params = {'contractId': 'G-2PLTYIP'},
    headers = {"accept": "application/vnd.akamai.cps.enrollments.v11+json"})

脚注