dynamodb的操作
查看项目
import json
import boto3
def lambda_handler(event, context):
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('user')
response = table.get_item(
Key={
'username': 'test'
}
)
print(response)
print(response['Item']['password'])
if 'Item' in response:
return response['Item']['password']
return None
删除项目
import json, boto3
def lambda_handler(event, context):
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('user')
table.delete_item(
Key={
'id': '1'
}
)
查询和浏览
查询
```python
import json, boto3
from boto3.dynamodb.conditions import Key, Attr
def lambda_handler(event, context):
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('user')
response = table.query(
KeyConditionExpression=Key('name').eq('熊二')
)
items = response['Items']
print(items)
扫描
import json, boto3
from boto3.dynamodb.conditions import Key, Attr
def lambda_handler(event, context):
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('user')
response = table.scan(
FilterExpression=Attr('age').eq('17')
)
items = response['Items']
print(items)
上传新项目
import json
import boto3
def lambda_handler(event, context):
client = boto3.client('dynamodb')
response = client.put_item(
Item={
'username': {
'S': 'test',
},
'password': {
'S': '666',
}
},
TableName='user',
)
print(response)
更新项目
import json, boto3
def lambda_handler(event, context):
client = boto3.client('dynamodb')
response = client.update_item(
Key={
'id': {
'S': '1',
}
},
AttributeUpdates={
'name': {
'Value': {
'S': 'haha',
}}
},
TableName='user',
)
print(response)