gpt4 book ai didi

python - AWS EC2 Boto3 中的 "start_instances() only accepts keyword arguments"错误

转载 作者:行者123 更新时间:2023-11-28 21:39:13 25 4
gpt4 key购买 nike

我正在尝试使用 boto3 启动一个 EC2 实例。当我执行下面的代码时,它工作正常

import boto3


ec2client = boto3.client('ec2')

class StartInstances:

def start_ec_instances(self):
response = ec2client.start_instances(InstanceIds=['i-XXXXXXXXXX'])
return

StartInstances().start_ec_instances()

但是当我运行下面的代码时,我得到了

import boto3


ec2client = boto3.client('ec2')

class StartInstances:

def start_ec_instances(self, instanceid):
response = ec2client.start_instances(instanceid)
return

StartInstances().start_ec_instances('InstanceIds=[\'i-XXXXXXXXXX\']')

Traceback (most recent call last): File "/Users/xxx/PycharmProjects/ctm-scripting-utils/ec2/start_instances.py", line 25, in StartInstances().start_ec_instances("InstanceIds=[\'i-XXXXXXXXXX\']") File "/Users/xxx/PycharmProjects/ctm-scripting-utils/ec2/start_instances.py", line 11, in start_ec_instances response = ec2client.start_instances(instanceids) File "/Users/xxx/Library/Python/3.6/lib/python/site-packages/botocore/client.py", line 310, in _api_call "%s() only accepts keyword arguments." % py_operation_name) TypeError: start_instances() only accepts keyword arguments.

最佳答案

更多的是 Python 问题。您正在尝试传递 string:'InstanceIds=[\'i-XXXXXXXXXX\']' 而不是 kwargs:InstanceIds =[..]。一种可能的修复方法是:

class StartInstances:
def start_ec_instances(self, instanceid):
response = ec2client.start_instances(InstanceIds=[instanceid])
return

StartInstances().start_ec_instances('i-XXXXXXXXXX')

关于python - AWS EC2 Boto3 中的 "start_instances() only accepts keyword arguments"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47147780/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com