Boto3访问AWS资源操作总结(1)
最近在工作中需要对AWS上的部分资源进行查询和交叉分析,虽然场景都比较简单,但是这种半机械的工作当然还是交给Python来搞比较合适。AWS为Python提供的SDK库叫做boto3,所以我们建立一个Python项目,Interpreter选择的是venv解析,再将boto3安装到项目中,下面就可以开始愉快地写代码了。这个过程中有一些坑,记录在这里,以便后续查阅。
Query AWS CloudWatch
根据一定的搜索条件去CloudWatch中查找相关的log记录。
import boto3
def query_cloudwatch_with_condition(log_group, query, start_time, end_time):
"""
Search CloudWatch logs by some conditions.
:param log_group: eg. '/aws/some_log_group'
:param query: eg. f"fields @timestamp, @message \
| sort @timestamp desc \
| filter @message like /(?i)(some_filter)/ \
| filter @message like /Reason:\sError:/ \
| limit 10 \
| display @message"
:param start_time: eg. int((datetime.today() - timedelta(days=5)).timestamp())
:param end_time: eg. int(datetime.now().timestamp())
:return: log message string.
"""
cw_client = boto3.client('logs')
start_query_response = cw_client.start_query(
logGroupName=log_group,
startTime=start_time,
endTime=end_time,
queryString=query,
)
query_id = start_query_response['queryId']
response = None
# NOTE: Must wait for query to complete.
while response is None or response['status'] == 'Running':
print('Waiting for query to complete ...')
time.sleep(1)
response = cw_client.get_query_results(queryId=query_id)
issue_detail = ''
# NOTE: In my situation, we only care about the first message because we expect all logs are the same.
for item in response['results'][0]:
if item['field'] == '@message':
issue_detail = item['value']
break
return issue_detail
Query DynamoDB
import boto3
from boto3.dynamodb.conditions import Key
def query_dynamodb_with_condition(key_conditionn_exp):
"""
Query dynamodb with certain condition_exp (Query not Scan)
:param key_conditionn_exp: eg. Key('id').eq(certain_id) & Key('sk').begins_with('example::')
:return: query results list
"""
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('some-dynamodb-name')
response = table.query(KeyConditionExpression=key_conditionn_exp)
items = response['Items']
# filter item if we have further conditions.
for item in items:
pass
return items
Scan DynamoDB
对DynamoDB做scan的时候,有个坑是AWS的DynamoDB单次scan是有上限的,所以为了做到full scan,需要在代码里面有一些处理
def scan_dynamodb_with_condition(filter_condition_exp):
"""
Full scan dynamodb with certain condition_exp
:param filter_condition_exp: eg. Attr('sk').eq('my_sk') & Attr('name').begins_with('Jone') & Attr('isDeleted').eq(False)
:return: scan results list
"""
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('some-dynamo-table')
response = table.scan(FilterExpression=filter_condition_exp)
# Loop to do full scan
results = response['Items']
index = 1
while 'LastEvaluatedKey' in response:
print(f'scanning....{index}')
index += 1
response = table.scan(
ExclusiveStartKey=response['LastEvaluatedKey'],
FilterExpression=filter_condition_exp)
results.extend(response['Items'])
print(len(results))
return results
List S3 objects and read contents
读取S3某个路径下的所有objects也有一个坑,就是默认单次get object的上限是1000个,所以如果想做到full list,也需要做特定的处理。
def get_all_s3_objects(s3, **base_kwargs):
"""
Private method to list all files under path
:param s3: s3 client using boto3.client('s3')
:param base_kwargs: scan args
:return: yield file path to caller
"""
continuation_token = None
while True:
list_kwargs = dict(MaxKeys=1000, **base_kwargs)
if continuation_token:
list_kwargs['ContinuationToken'] = continuation_token
response = s3.list_objects_v2(**list_kwargs)
yield from response.get('Contents', [])
if not response.get('IsTruncated'): # At the end of the list?
break
continuation_token = response.get('NextContinuationToken')
def main():
bucket_name = 'my-bucket-name'
s3_client = boto3.client('s3')
# using prefix to define search folder
prefix = 'this-is-some-path-without-prefix-and-postfix-slash'
file_paths = []
for file in get_all_s3_objects(s3_client, Bucket=bucket_name, Prefix=prefix):
file_paths.append(file['Key'])
print(f'length of file_paths: {len(file_paths)}')
with open('./file_paths_results.json', 'w') as f:
f.write(json.dumps(file_paths))
print('finished writing file paths into json file')
Read S3 file contents
在读取S3文件的内容时,我们遇到了文件Body里的内容(来自AWS SQS的message)无法正确的转换为json的问题,因为时间问题,没有太深入地研究,只是简单地做了一些非json语法字串的替换,把内容拿出来了,后面可以再研究一下这种文件内容需要怎么正确加载到json里。
import json
import re
from pprint import pprint
import boto3
from dynamodb_json import json_util
def read_file_contents(s3client, bucket, path):
"""
Read a file content with it's key (filepath)
:param s3client: eg. boto3.client('s3')
:param bucket: eg. 'some-bucket-name'
:param path: eg. 'some-path-to-my-file-with-postfix-no-slash-prefix'
:return: file contents in json format
"""
file_obj = s3client.get_object(
Bucket=bucket,
Key=path)
# open the file object and read it into the variable filedata.
file_data = file_obj['Body'].read()
# TODO: we did some ugly string replace here.. will fix this later
print_str = json_util.loads(file_data).replace('\\', '').replace('""', '"').replace('"Body":"', '"Body":').replace(
'}}}"}', '}}}}').replace('= "', '- ').replace('" Or', ' -').replace('" And', ' -')
json_obj = json_util.loads(print_str)
# NOTE: we use regex to match what we want.
# match = re.findall('someKey":{"S":"(.*?)"', print_str)
# if match:
# pprint(f'find key: {match[0]}')
# return match[0]
# else:
# print(f'no key found!')
# return None
return json_obj
本文作为此次生产环境数据问题Investigate的解决过程,记录在这里,数据已经经过脱敏,请结合自己的实际环境进行配置。
Boto3访问AWS资源操作总结(1)的更多相关文章
- [原创]java WEB学习笔记55:Struts2学习之路---详解struts2 中 Action,如何访问web 资源,解耦方式(使用 ActionContext,实现 XxxAware 接口),耦合方式(通过ServletActionContext,通过实现 ServletRequestAware, ServletContextAware 等接口的方式)
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- Struts2在Action中访问WEB资源
什么是WEB资源? 这里所说的WEB资源是指:HttpServletRequest, HttpSession, ServletContext 等原生的 Servlet API. 为什么需要访问WEB资 ...
- 在centos7中限制kvm虚拟机可访问的资源
最近通过艰苦卓绝的度娘(我很想用谷歌,可是,你懂的),终于搞明白如何在centos7中限制kvm虚拟机可访问的资源了.度娘给出的结果中,大部分都说的很对,然而,却很难照着做,主要原因有两点:1.网上的 ...
- Struts2 之 Action 类访问 WEB 资源
接着上次博客的内容我继续分享我所学到的知识,和自己在学习过程中所遇到问题以及解决方案.当然,如果读者发现任何问题均可以在下方评论告知我,先谢! 在 Action 中访问 WEB 资源 web 资源 所 ...
- SpringMVC拦截器实现:当用户访问网站资源时,监听session是否过期
SpringMVC拦截器实现:当用户访问网站资源时,监听session是否过期 一.拦截器配置 <mvc:interceptors> <mvc:interceptor> < ...
- hystrix完成对redis访问的资源隔离
相对来说,考虑的比较完善的一套方案,分为事前,事中,事后三个层次去思考怎么来应对缓存雪崩的场景 1.事前解决方案 发生缓存雪崩之前,事情之前,怎么去避免redis彻底挂掉 redis本身的高可用性,复 ...
- 零基础学习java------38---------spring中关于通知类型的补充,springmvc,springmvc入门程序,访问保护资源,参数的绑定(简单数据类型,POJO,包装类),返回数据类型,三大组件,注解
一. 通知类型 spring aop通知(advice)分成五类: (1)前置通知[Before advice]:在连接点前面执行,前置通知不会影响连接点的执行,除非此处抛出异常. (2)正常返回通知 ...
- java在访问https资源时,忽略证书信任问题 (转)
java程序在访问https资源时,出现报错sun.security.validator.ValidatorException: PKIX path building failed: sun.secu ...
- spring访问静态资源出错,No mapping found for HTTP request with URI xxx/resources/js/jquery.min.js...
问题:spring访问静态资源出错,No mapping found for HTTP request with URI xxx/resources/js/jquery.min.js... web.x ...
随机推荐
- [noi1994]海盗
令$a_{i,j}(j\le i)$表示第i个人的方案中给第j个人$a_{i,j}$的钱,有以下性质: 1.如果第j个人一定同意(否则就会死)第i个人的方案,那么$a_{i,j}=0$(容易发现一定同 ...
- 《手把手教你》系列技巧篇(四十五)-java+ selenium自动化测试-web页面定位toast-上篇(详解教程)
1.简介 在使用appium写app自动化的时候介绍toast的相关元素的定位,在Web UI测试过程中,也经常遇到一些toast,那么这个toast我们这边如何进行测试呢?今天宏哥就分两篇介绍一下. ...
- 模版 动态 dp
模版 动态 dp 终于来写这个东西了.. LG 模版:给定 n 个点的数,点有点权, $ m $ 次修改点权,求修改完后这个树的最大独立集大小. 我们先来考虑朴素的最大独立集的 dp \[dp[u][ ...
- 回文字符串 Manacher
1. Manacher 忘光了,忘光了. 首先将字符串所有字符之间(包括头尾)插入相同分隔符,再在最前方插入另一个分隔符防止越界. 设以 \(s_i\) 为对称中心的回文串中,最长的回文半径为 \(p ...
- python3 excel读、写、修改操作
python3上Excel文件操作的库比较多,新手一开始不知道如何选择合适的库,故整理如下: xlwt: 只能写不能读,只支持python2.3到python2.7版本,只支持xls文件. xlrd ...
- Mysql的delimiter
告诉MySQL解释器,该段命令是否已经结束了,mysql是否可以执行了.默认情况下,delimiter是分号;.在命令行客户端中,如果有一行命令以分号结束,那么回车后,mysql将会执行该命令. 有时 ...
- APP工程师接入Telink Mesh流程 -3
加密是为了使网络更加的安全.健壮,若由于login.加密等流程 严重影响了 开发进程,也可以通过 修改SDK 固件 将login.加密 环节取消 1.发送数据.接受数据加密,解密去掉 mesh_sec ...
- php代码审计入门前必看
首先先介绍什么是代码审计? 代码审计:是指针对源代码进行检查,寻找代码中的bug,这是一项需要多方面技能的技术 包括:对编程的掌握,漏洞形成原理的理解,系统和中间件等的熟悉 2.为什么要进行代码审计, ...
- Prometheus概述
Prometheus是什么 首先, Prometheus 是一款时序(time series) 数据库, 但他的功能却并非支部与 TSDB , 而是一款设计用于进行目标 (Target) 监控的关键组 ...
- 7个连环问揭开java多线程背后的弯弯绕
摘要:很多java入门新人一想到java多线程, 就会觉得很晕很绕,什么可见不可见的,也不了解为什么sync怎么就锁住了代码. 本文分享自华为云社区<java多线程背后的弯弯绕绕到底是什么? 7 ...