aws python sdk 常用操作

环境信息

  • centos 7
  • Python3.10
  • boto3 1.23.2

    (pip3 install boto3)

S3 相关操作

获取 s3 中文件大小

from boto3.session import Session

ak = 'XXXXXXX'
sk = 'XXXXXXXXXX'
region = 'us-west-1'
bucketName = 'testBucket'

session = Session(aws_access_key_id=ak, aws_secret_access_key=sk, region_name=region)
s3 = session.resource('s3')

bucket = s3.Bucket(bucketName)
obj = bucket.objects.filter(Prefix='filename')
for i in obj:
if i.size:
return i.size

EC2 相关操作

获取 EC2 instances

import boto3

client = boto3.client('ec2',region_name='ap-east-1',aws_access_key_id='AK',aws_secret_access_key='ty')

# 列出所有 Instances
client.describe_instances()