0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

boto3でDynamoDBを操作する

Posted at

はじめに

DynamoDBをboto3で操作するとき、
登録・更新・削除の処理を書くことが多く、そのメモを残します。

ソースコード

登録

put.py
def put_item(param):
    try:
        table = dynamo.Table(config.TABLE)
        response = table.put_item(
            Item=param
        )
    except Exception as e:
        logger.exception(e)
        raise

更新

update.py
def update_item(param):
    table = dynamo.Table(config.TABLE)
    try:
        update_params = {
            'Key': {
                'primary_key': primary_key,
                'secodary_key': secodary_key
            },
            'UpdateExpression' : "SET #st = :d",
            'ExpressionAttributeNames': {
                "#st" : "update_column"
            },
            'ExpressionAttributeValues': {
                ":d": param
            }
        }
        response = table.update_item(**update_params)
    except Exception as e:
        logger.exception(e)
        raise

attribute 削除

delete_attribute.py
def delete_attribute(param):
    table = dynamo.Table(config.TABLE)
    try:
        table.update_item(
            Key= {
                'primary_key': primary_key,
                'secodary_key': secodary_key
            },
            UpdateExpression='remove foo' # foo というキーのデータを削除する
        )
    except Exception as e:
        logger.exception(e)
        raise

削除

delete.py
def delete_item(id):
    table = dynamo.Table(config.TABLE)
    try:
        table.delete_item(Key={'year': year, 'title': title})
    except Exception as e:
        logger.exception(e)
        raise
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?