首页 > 编程语言 >aws dynamodb python boto3 调用方法

aws dynamodb python boto3 调用方法

时间:2022-11-20 10:55:34浏览次数:43  
标签:dynamodb python self aws boto3 item dict id

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}')

标签:dynamodb,python,self,aws,boto3,item,dict,id
From: https://www.cnblogs.com/jneeee/p/dynamodb_demo.html

相关文章

  • Python OpenCV给证件照换底色
    ✅作者简介:热爱科研的算法开发者,Python、Matlab项目可交流、沟通、学习。......
  • 力扣744(java&python)- 寻找比目标字母大的最小字母(简单)
    题目:给你一个排序后的字符列表letters,列表中只包含小写英文字母。另给出一个目标字母 target,请你寻找在这一有序列表里比目标字母大的最小字母。在比较时,字母是依序循......
  • Python3-实战
    实战01(模拟支付宝蚂蚁森林的能量产生过程)1print("查询能量请输入能量来源!退出程序请输入0")2source=input("能量来源如下:\n生活缴费,行走捐,共享单车,线下支付,网......
  • python面试题常用语句
    一、比较与交换1.比较并输出大的print(aifa>belseb)2.交换两个元素a,b=b,alist1[i],list[j]=list1[j],list[i] 二、排序1.字符串排序s='aaccbgd'pri......
  • python课本学习第六章
    一、字典的概念#示例代码student={'name':'xx','name':'yy','grade1':98.1,'grade':99.2}print(student)#output:{'name':'yy','grade1':98.1,'grade':99.2}字典的......
  • AWS资源注解
    EC2(ElasticComputeCloud)ECS(ElasticContainerService)ECR(ElasticContainerRegistry)Lambdahttps://aws.amazon.com/lambda/RunyourCodeinResponset......
  • python爬虫某东商品销售数据
    经典四步曲:1、查找有效URL,分析是静态数据还是动态数据,分析数据的格式。2、编写python代码,向服务器发送请求,获取数据。3、解析数据4、保存数据importrequestsimportjson......
  • python中的函数(1)
     #1.函数#函数就是将一段具有独特功能的代码段整合到一个整体并命名#在需要的位置调用这个名称即可完成对应的需求#函数的作用:封装代码(方便管理),实现代码重用......
  • Python程序员:代码写的好,丝滑的壁纸少不了
    不知道大家的电脑桌面一般用的什么类型的壁纸?早上来上班,打开电脑,被漂亮的桌面壁纸所吸引,年底将近,这又是哪个地方的节日?才晓得,原来这是泰国第二大城市清迈的“天......
  • python ddddocr图片验证码详解
     下载地址:https://pypi.tuna.tsinghua.edu.cn/simple/ddddocr/安装命令:pipinstallD:\ChromeCoreDownloads\ddddocr-1.3.0-py3-none-any.whl-ihttps://pypi.tuna.ts......