您没有订阅此服务

2024-04-20 02:10:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我想用boto3来读取现货价格。我添加了一个使用iampaws的用户,并为他提供了使用策略

  • EC2服务
  • 行动:
  • 描述PotPriceHistory+StartInstances+StopInstances

我用awscli在我的UBUNTU机器上配置aws

当我运行简单的例子来获取现货价格时,我有以下错误:

ClientError: An error occurred (OptInRequired) when calling the DescribeSpotPriceHistory operation: You are not subscribed to this service. Please go to http://aws.amazon.com to subscribe.

代码:

import boto3
client = boto3.client(
    'ec2',region_name='us-east-1',
    aws_access_key_id=ACCESS_KEY,
    aws_secret_access_key=SECRET_KEY,
)

prices=client.describe_spot_price_history(InstanceTypes=['m3.medium'],MaxResults=1,ProductDescriptions=['Linux/UNIX (Amazon VPC)'],AvailabilityZone='us-east-1')

print prices['SpotPriceHistory'][0]

Tags: tokey用户clientawsaccess价格boto3
1条回答
网友
1楼 · 发布于 2024-04-20 02:10:05

嗯,您正在进行的describe_spot_price_history()调用有两个问题:

  1. AvailabilityZone='us-east-1'错误。必须指定AZ:us-east-1a或{}等,而不是区域us-east-1。当我尝试的时候,这给了我一个ClientError: ... InvalidParameterValue。在
  2. 也许这确实是您想要的,但是请注意,使用MaxResults=1您实际上只会从调用中获得一个数据点,这可能不是非常有用。您必须遍历剩余的页面,以获取剩余的价格历史记录。我不确定是否有任何明确的保证,即检索到的第一个结果始终是此价格历史记录的最新数据点(尤其是因为您没有指定StartTime或{}),因此,如果代码的目的是这样的话,这可能是不可靠的。在

除了这些问题,这个呼吁应该是有效的。仔细检查您提供的访问密钥、您正在使用的IAM用户的策略,并根据@krishna-kumar-r的评论建议,联系支持部门,以防您的AWS帐户可能拖欠。在

相关问题 更多 >