Python 中的 AWS Boto 入门
Maksim Pecherskiy
Data Engineer
![]()
![]()



主题设置
获取汇总数据
发送警报
初始化 SNS 客户端
sns = boto3.client('sns',
region_name='us-east-1',
aws_access_key_id=AWS_KEY_ID,
aws_secret_access_key=AWS_SECRET)
创建主题并保存其 ARN
trash_arn = sns.create_topic(Name="trash_notifications")['TopicArn']
streets_arn = sns.create_topic(Name="streets_notifications")['TopicArn']

contacts = pd.read_csv('http://gid-staging.contacts.csv')
| Name | Phone | Department | |
|---|---|---|---|
| John Smith | [email protected] | +11224567890 | trash |
| Fanny Mae | [email protected] | +11234597890 | trash |
| Janessa Goldsmith | [email protected] | +11534567890 | streets |
| Evelyn Monroe | [email protected] | +11234067890 | streets |
| Max Pe | [email protected] | +11234517890 | streets |
创建 subscribe_user 方法
def subscribe_user(user_row):if user_row['Department'] == 'trash': sns.subscribe(TopicArn = trash_arn, Protocol='sms', Endpoint=str(user_row['Phone'])) sns.subscribe(TopicArn = trash_arn, Protocol='email', Endpoint=user_row['Email'])else: sns.subscribe(TopicArn = streets_arn, Protocol='sms', Endpoint=str(user_row['Phone'])) sns.subscribe(TopicArn = streets_arn, Protocol='email', Endpoint=user_row['Email'])
将 subscribe_user 应用于每一行
contacts.apply(subscribe_user, axis=1)

将一月报告加载为 DataFrame
df = pd.read_csv('http://gid-reports.2019/feb/final_report.csv')
| service_name | count |
|---|---|
| Illegal Dumping | 2580 |
| Potential Missed Collection | 150 |
| Pothole | 1170 |
| Traffic Sign - Maintain | 210 |
| Traffic Signal Head Turned | 60 |
| Traffic Signal Light Out | 120 |
设置索引,以服务名直接取数
df.set_index('service_name', inplace=True)
获取汇总数据
trash_violations_count = df.at['Illegal Dumping', 'count']
streets_violations_count = df.at['Pothole', 'count']
if trash_violations_count > 100:# Construct the message to send message = "Trash violations count is now {}".format(trash_violations_count)# Send message sns.publish(TopicArn = trash_arn, Message = message, Subject = "Trash Alert")
if streets_violations_count > 30: # Construct the message to send message = "Streets violations count is now {}".format(streets_violations_count)# Send message sns.publish(TopicArn = streets_arn, Message = message, Subject = "Streets Alert")

Python 中的 AWS Boto 入门