aws dynamodb python boto3 调用方法

dynamodb = boto3.resource('dynamodb') 是boto3 的高级接口,更底层的有:

import boto3

dynamodb_client = boto3.client('dynamodb')
res = dynamodb_client.list_tables()

高级接口调用参考如下,其中 'id' 是我设置的主键,没设置排序键:

import time
import logging

import boto3

LOG = logging.getLogger(__name__)


class Tableclient():
    def __init__(self, tablename) -> None:
        dynamodb = boto3.resource('dynamodb')
        self.table = dynamodb.Table(tablename)

    def get(self, item):
        # item: dict{'id': key}
        if not isinstance(item, dict):
            return False
        resp = None
        try:
            resp = self.table.get_item(Key=item).get("Item")
        except Exception as e:
            LOG.error(e)
        return resp

    def delete(self, item):
        # item: dict{'id': key}
        if not isinstance(item, dict):
            return False
        self.table.delete_item(Item=item)

    def put_item(self, item):
        # item: dict{'id': id, 'value': value}
        if not isinstance(item, dict):
            return False
        self.table.put_item(Item=item)
        LOG.info(f'Put_item: {item}')

我用 aws 免费配额写了个定时任务框架(lambda+dynamodb),通过github action部署到aws。地址 https://github.com/jneeee/taskbox

参考 https://maku.blog/p/wht5epz/

posted @ 2022-11-20 10:46  Jneeee  阅读(170)  评论(0编辑  收藏  举报