亚马逊api调用

一、获取资产

import boto3

aws_access_key_id = 'XXX'
aws_secret_access_key = 'XXX'
region_name = 'XXX'

session = boto3.session.Session(aws_access_key_id = aws_access_key_id,
                                aws_secret_access_key = aws_secret_access_key,
                                region_name = region_name)

ec2 = session.resource('ec2')
for instance in ec2.instances.all():
    print(instance.instance_id)

二、获取账单文件

1、控制台配置可读权限, api 地址如下:

https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3.html#S3.Bucket.download_file

import boto3
s3 = boto3.resource('s3',aws_access_key_id="",aws_secret_access_key="",region_name='')
s3.Bucket('bucket').download_file('Key','Filename')
  • Key(str) -- 下载的文件名
  • Filename(str) -- 下载到本地的路径

三、获取账单明细

import csv
with open(bill_file,'r') as csvfile:
    # 第一行是说明,跳过
    next(csvfile)
    reader = csv.DictReader(csvfile)
    for row in reader:
        # 只获取有productcode 的数据
        if row['ProductCode']:
            pass

你可能感兴趣的:(亚马逊api调用)