boto3
botoの名前の由来
hiroki.iconなんかピンとこない略だよなと思ってた
boto = Amazon river dolphin
アマゾン川に生息するアマゾンカワイルカにちなんでbotoとなずけられた
botoを利用することでawsエコシステムに簡単に触れることができるという意味が込められているらしい
クレデンシャルの取得について
8か所から規定の順序で認証情報が検索される
ローカルのPython環境でboto3にIAMロールが渡るようにする
assume-role rolename python
S3
hiroki.iconクエリとかめちゃくちゃ参考になる記事
code:python
import boto3
from boto3.dynamodb.conditions import Key, Attr
dynamo_client = boto3.resource("dynamodb")
table = dynamo_client.Table("SampleTable")
query_result = table.query(
KeyConditionExpression=Key('date').eq(...) & Key('timestamp').between(start, end)
)
query_result2 = table.query(
IndexName="IndexName".
KeyConditionExpression=Key('date').eq(...) & Key('timestamp').between(start, end)
)
res = table.put_item(
Item={
'uuid': ...,
'name': ...,
}
)
table.key_schema
table.global_secondary_indexes
aws公式のboto3でのquery例
Queryのレスポンス例
code:json
{
'Items':
[{'uuid': 'd700241d-f149-4267-abd0-bbdef3a5b37b', 'date': '2021-03-25', 'timestamp': Decimal('1616676100')}, {'uuid': '428a4fb0-70c5-43f1-830a-be46ee6928f5', 'date': '2021-03-25', 'timestamp': Decimal('1616676116')}, {'uuid': 'ea5563a1-cc12-48ba-b4e1-e0a06a2510c2', 'date': '2021-03-25', 'timestamp': Decimal('1616676117')},
{'uuid': 'f031d571-03b0-47ee-830e-47a02df6e163', 'date': '2021-03-25', 'timestamp': Decimal('1616676118')}, {'uuid': 'b85b0266-9b8a-4aa9-ad7d-31de321aebf3', 'date': '2021-03-25', 'timestamp': Decimal('1616676118')}],
'Count': 5,
'ScannedCount': 5,
'ResponseMetadata':
{'RequestId': 'HC94ORGGS0VN1GIS3D7TN6K5DJVV4KQNSO5AEMVJF66Q9ASUAAJG', 'HTTPStatusCode': 200, 'HTTPHeaders': {'server': 'Server', 'date': 'Thu, 25 Mar 2021 12:50:57 GMT', 'content-type': 'application/x-amz-json-1.0', 'content-length': '593', 'connection': 'keep-alive', 'x-amzn-requestid': 'HC94ORGGS0VN1GIS3D7TN6K5DJVV4KQNSO5AEMVJF66Q9ASUAAJG', 'x-amz-crc32': '3900119517'},
'RetryAttempts': 0}
}
for item in query_result['Items']:みたいな感じで取り出すことになる
update
AttributeUpdatesはレガシーなので使わない
code:python
def handle_update(params):
res =DYNAMO_TABLE.update_item(
Key={
"article_id": article_id
},
UpdateExpression="SET title = :title",
ExpressionAttributeValues={
}
)
SSM