You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Boto3调用get_metric_data报错:仅接受关键字参数,动态传参失败

解决get_metric_data()仅接受关键字参数的错误

问题出在参数传递方式上:boto3的get_metric_data()方法要求所有参数必须以关键字参数形式传入,但你现在是把metrics_input字典作为单个位置参数直接传递,这就触发了TypeError。

修改后的代码

def get_metrics(metrics_input):
    client = boto3.client(service_name='dms', region_name='us-east-1')
    # 使用**解包字典,将键值对转为关键字参数
    response = client.get_metric_data(**metrics_input)
    return response

原理说明

当你动态构建的metrics_input是标准字典(比如{'MetricDataQueries': [...], 'StartTime': ...}结构)时,**metrics_input会自动把字典里的每个键值对拆解成key=value的形式,完全匹配get_metric_data()对关键字参数的要求,和你手动把参数写在调用里的效果一致。

内容的提问来源于stack exchange,提问作者Algoritmi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 12:00:21