问题描述
如何在不登陆火山引擎控制台的情况下,统计目前创建的实例总数及相对应信息? 可以通过调用OpenAPI的方法,发送GET/POST请求。执行的接口动作为:DescribeVolumes。经过计算签名,确认用户合法身份后,开始返回响应参数,构建写入文件。
示例代码
本文以python作为示例代码说明。
import sys, os, base64, datetime, hashlib, hmac
import requests # pip install requests
import datetime
import json
import csv, codecs
import math
from requests.models import codes
def sign(key, msg): #定义sign函数
return hmac.new(key, msg.encode('utf-8'), hashlib.sha256).digest() #返回一些带格式的参数
def getSignatureKey(key, dateStamp, regionName, serviceName): #自定义函数getSigatureKey
kDate = sign(key.encode('utf-8'), dateStamp)
kRegion = sign(kDate, regionName)
kService = sign(kRegion, serviceName)
kSigning = sign(kService, 'request')
return kSigning #返回kSinging
def formatParameters(parameters): #定义formatParameters(参数格式)函数
request_parameters_init = ''
for key in sorted(parameters): #循环函数
request_parameters_init += key + '=' + parameters[key] + '&'
request_parameters = request_parameters_init[:-1]
return request_parameters
def sigv4(access_key, secret_key, service, request_parameters, method, host, region, endpoint, payload): #定义sigv4函数
if access_key is None or secret_key is None: #条件判断函数,如果AK/SK有任一一个为空,输出以下提示
print('No access key is available.')
sys.exit()
t = datetime.datetime.utcnow()
current_date = t.strftime('%Y%m%dT%H%M%SZ') #设置时间的精确度
# current_date = '20210818T095729Z'
datestamp = t.strftime('%Y%m%d') # Date w/o time, used in credential scope
canonical_uri = '/'
canonical_querystring = request_parameters
signed_headers = 'content-type;host;x-content-sha256;x-date' #响应头:内容格式,主机名,日期
payload_hash = hashlib.sha256(payload.encode('utf-8')).hexdigest()
content_type = 'application/x-www-form-urlencoded; charset=utf-8' #字符集是UTF-8格式
canonical_headers = 'content-type:' + content_type + '\n' + 'host:' + host + '\n' + 'x-content-sha256:' + payload_hash + '\n' + 'x-date:' + current_date + '\n'
canonical_request = method + '\n' + canonical_uri + '\n' + canonical_querystring + '\n' + canonical_headers + '\n' + signed_headers + '\n' + payload_hash
algorithm = 'HMAC-SHA256'
credential_scope = datestamp + '/' + region + '/' + service + '/' + 'request'
string_to_sign = algorithm + '\n' + current_date + '\n' + credential_scope + '\n' + hashlib.sha256(canonical_request.encode('utf-8')).hexdigest()
signing_key = getSignatureKey(secret_key, datestamp, region, service)
signature = hmac.new(signing_key, (string_to_sign).encode('utf-8'), hashlib.sha256).hexdigest()
authorization_header = algorithm + ' ' + 'Credential=' + access_key + '/' + credential_scope + ', ' + 'SignedHeaders=' + signed_headers + ', ' + 'Signature=' + signature
headers = {'X-Date': current_date,
'Authorization':authorization_header,
'X-Content-Sha256': payload_hash,
'Content-Type': content_type
}
return headers, canonical_querystring #返回响应头,典型的请求参数
def writeRows(result, csv_writer):
# 格式化输出,包含云盘id、云盘名称、云盘状态、可用区、云盘类型、云盘属性、云盘挂载的实例id、计费类型、付费类型
for volume in result['Result']['Volumes']:
csvList = []
csvList.append(volume['VolumeId'])
csvList.append(volume['VolumeName'])
csvList.append(volume['Status'])
csvList.append(volume['ZoneId'])
csvList.append(volume['VolumeType'])
csvList.append(volume['InstanceId'])
csvList.append(volume['Kind'])
csvList.append(volume['BillingType'])
csvList.append(volume['PayType'])
print("test2")
csv_writer.writerow(csvList)
def sendRequest(volc_access_key, volc_secret_key, serviceName, apiParameters, method, host, region, endpoint, payload):
# 格式化参数顺序
formatted_parameters = formatParameters(apiParameters)
# signatureV4签名
headers,canonical_querystring = sigv4(volc_access_key, volc_secret_key, serviceName, formatted_parameters, method, host, region, endpoint, payload)
request_url = endpoint + '?' + canonical_querystring
if method == 'GET': #如果请求方法为GET
r = requests.get(request_url, headers=headers)
elif method == 'POST': #如果为POST
r = requests.post(request_url, data=payload,headers=headers)
else:
print("Wrong HTTP method") #HTTP请求方法不可用
result = json.loads(r.text) #json格式的result
return result
#创建CVS文件,生成单独的表格文件,包含的参数内容如下
def createCSV(volc_access_key, volc_secret_key, serviceName, apiParameters, method, host, region, endpoint, payload, fileName):
# 写入csv文件
# 1. 创建文件对象
f = codecs.open(fileName, 'wb', "gbk")
# 2. 基于文件对象构建csv写入对象
csv_writer = csv.writer(f)
# 3. 构建列表头
csv_writer.writerow(['云盘id','云盘名称','云盘状态','可用区','云盘类型','云盘挂载的实例id','云盘属性','计费类型','付费类型'])
# 查看资源总数
#sendRequest发送请求,AK/SK/服务名/api参数/请求方法/主机名/地域/endpoint/payload
r = sendRequest(volc_access_key, volc_secret_key, serviceName, apiParameters, method, host, region, endpoint, payload)
totalCount = r['Result']['TotalCount']
if totalCount > 10 and totalCount <= 100: #每一张表格记录的资源总数最大为100
pageSize = 100
apiParameters['PageSize'] = str(pageSize)
r = sendRequest(volc_access_key, volc_secret_key, serviceName, apiParameters, method, host, region, endpoint, payload)
writeRows(r, csv_writer)
elif totalCount > 100:
pageSize = 100
apiParameters['PageSize'] = str(pageSize)
pageNumber = math.ceil(totalCount / 100) + 1 #如果资源总数超过100,超过100的部分放在第二张表格中
for index in range(1, pageNumber):
apiParameters['PageNumber'] = str(index)
r = sendRequest(volc_access_key, volc_secret_key, serviceName, apiParameters, method, host, region, endpoint, payload)
writeRows(r, csv_writer)
else:
r = sendRequest(volc_access_key, volc_secret_key, serviceName, apiParameters, method, host, region, endpoint, payload)
writeRows(r, csv_writer)
f.close()
if __name__=="__main__":
# 用户AKSK
volc_access_key = os.getenv('VOLC_ACCESS_KEY')
volc_secret_key = os.getenv('VOLC_SECRET_KEY')
region = os.getenv('VOLC_REGION')
# 发送OpenAPI必要
method = 'GET'
host = 'open.volcengineapi.com'
endpoint = 'https://open.volcengineapi.com'
serviceName = 'storage_ebs'
#获取
apiParameters = {
'Action': 'DescribeVolumes',
'Version': '2020-04-01'
}
payload = ''
fileName = 'describeVolumes.csv'
createCSV(volc_access_key, volc_secret_key, serviceName, apiParameters, method, host, region, endpoint, payload, fileName)
执行方法
- 在执行代码之前,先执行下列命令,下载requests包
pip3 install requests
- Bash命令行(Linux和MacOS操作系统)中执行以下命令,设置执行参数VOLC_REGION为实例所在的地域,目前可使用cn-beijing或cn-nantong
export VOLC_ACCESS_KEY=AKLTZDA1NzI1YTg0NzFiNGU2MWIzZTNiZTBxxxxxx
export VOLC_SECRET_KEY=Wm1ObE5EaGhNak0xT0Raa05ESTFOV0U1T1daaU9EZGhNRGczxxxxxx
export VOLC_REGION=cn-beijing/cn-nantong
Windows操作系统可在PowerShell中执行以下命令,设置执行参数VOLC_REGION为实例所在的地域,目前可使用cn-beijing或cn-nantong
$env:VOLC_SECRET_KEY="Wm1ObE5EaGhNak0xT0Raa05ESTFOV0U1T1daaU9EZGhNRGczTTJWa0xxxxxx"
$env:VOLC_ACCESS_KEY="AKLTZDA1NzI1YTg0NzFiNGU2MWIzZTNiZTBlZTQ2xxxxxx"
$env:VOLC_REGION="cn-beijing/cn-nantong"
- 在python3.6及以上版本环境下执行下列命令,如需下载python环境,下载地址可参考:python
python3 fileName.py #请确保执行此命令时,该路径下有目标Python文件
执行结果
- 生成一个表格文件:生成的文件将存储在与脚本文件同一目录下:
- 文件头信息如下: