最近更新时间:2024-08-12 10:30:24
引入
pip install requests-aws4auth
AWS签名demo
import requests
from requests_aws4auth import AWS4Auth
from urllib.parse import urlencode
def main():
ak = "Your AK" # 请替换为您的AccessKey
sk = "Your SK" # 请替换为您的SecretKey
service = "iam" # 访问的服务
region = "cn-beijing-6" # 访问的区域
auth = AWS4Auth(ak, sk, region, service)
params = {
# 固定参数
"Action": "ListUsers", # 接口名称
"Version": "2015-11-01", # 接口版本
# 接口参数
"Marker": "1",
"MaxItems": "100",
}
query_string = urlencode(params)
url = f"http://{service}.api.ksyun.com?{query_string}"
headers = {
"Accept": "application/json",
"Host": f"{service}.api.ksyun.com",
}
response = requests.get(url, auth=auth, headers=headers)
if response.status_code != 200:
raise Exception(f"Error: {response.status_code}, {response.text}")
print(response.status_code)
print(response.text)
if __name__ == "__main__":
main()
纯净模式