简体   繁体   English

更新 DynamoDB 表项的属性

[英]Update attributes of DynamoDB table item

While defining my DynamoDB table, I selected filename as the unique attribute or primary key.在定义我的 DynamoDB 表时,我选择了filename作为唯一属性或主键。 In this function, I want to modify some columns of the entry with a particular filename .在这个 function 中,我想用特定的filename修改条目的一些列。 I tried this after following another SO answer:在遵循另一个 SO 答案后,我尝试了这个:

def update_item_in_db(filename, archive_path):
    table = dynamodb.Table(dynamodb_table_name)
    try:
        logger.info(f'Updating dynamo table for filename: {filename}')
        response = table.update_item(
            Key={'filename': filename},
            AttributeUpdates={
                'archive_path': archive_path,
                'archived_on': datetime.today().strftime('%Y-%m-%d-%H:%M:%S')
            },
        )
    except Exception as e:
        raise Exception(f"Unable to insert filename into dynamodb: {e}") 
    return response

but I get an error that:但我收到一个错误:

Parameter validation failed:\nInvalid type for parameter AttributeUpdates.archive_path, value: archive/100-ff0-uat/1591282803734/issues_1591282803734.zip, type: <class 'str'>, valid types: <class 'dict'>\nInvalid type for parameter AttributeUpdates.archived_on, value: 2021-11-21-15:06:46, type: <class 'str'>, valid types: <class 'dict'>",

Then I also tried:然后我也尝试了:

        response = table.update_item(
            {
                'filename': filename,
                'archive_path': archive_path,
                'archived_on': datetime.today().strftime('%Y-%m-%d-%H:%M:%S')
            }
        )

but then I get the following error:但后来我收到以下错误:

 "Unable to insert filename into dynamodb: update_item() only accepts keyword arguments.",

What's the correct syntax to update an item?更新项目的正确语法是什么?

Note that I only want to UPDATE/MODIFY and not delete and re-insert.请注意,我只想更新/修改而不是删除和重新插入。

The first approach uses a deprecated syntax.第一种方法使用不推荐使用的语法。 The second one is closer to what you want to do.第二个更接近你想要做的事情。

Something like this should work:像这样的东西应该工作:

from datetime import datetime

import boto3

dynamodb = boto3.resource("dynamodb")

def update_item_in_db(filename, archive_path):
    table = dynamodb.Table(dynamodb_table_name)

    table.update_item(
        Key={
            "filename": filename
        },
        UpdateExpression="SET #archive_path = :archive_path, #archived_on = :archived_on",
        ExpressionAttributeNames={
            "#archive_path": "archive_path",
            "#archived_on": "archived_on"
        },
        ExpressionAttributeValues={
            ":archive_path": archive_path,
            ":archived_on": datetime.today().strftime('%Y-%m-%d-%H:%M:%S')
        }
    )

The ExpressionAttributeNames and ExpressionAttributeValues allow you to use reserved keywords in the UpdateExpression , that's why they're commonly used. ExpressionAttributeNamesExpressionAttributeValues允许您在UpdateExpression中使用保留关键字,这就是它们常用的原因。

(I didn't run the code, there may be typos.) (我没有运行代码,可能有错别字。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM