sln*_*d54 5 python-3.x amazon-dynamodb slack-api aws-lambda
我在Python中使用boto3库中的查询函数并收到以下错误:
name 'Key' is not defined: NameError
Traceback (most recent call last):
File "/var/task/lambda_function.py", line 51, in lambda_handler
if not getAssetExistance(slack_userID):
File "/var/task/lambda_function.py", line 23, in getAssetExistance
response = dynamoTable.query(KeyConditionExpression=Key('userID').eq(asset))
NameError: name 'Key' is not defined
Run Code Online (Sandbox Code Playgroud)
我一直在阅读有关通过Lambda访问DynamoDB的一系列教程,并且在尝试密钥存在时,他们都使用此KeyConditionExpression行.
这是相关代码(第23行是查询行):
def getAssetExistance(asset):
dynamoTable = dynamo.Table('Assets')
response = dynamoTable.query(KeyConditionExpression=Key('userID').eq(asset))
return bool(response)
Run Code Online (Sandbox Code Playgroud)
我基本上想检查我的DynamoDB表中的主分区键(这是一个松弛的用户ID),看看是否存在.
如果相关,则以下是其余代码:
################################
# Slack Lambda handler.
################################
import boto3
import logging
import os
import urllib
# Grab data from the environment.
BOT_TOKEN = os.environ["BOT_TOKEN"]
ASSET_TABLE = os.environ["ASSET_TABLE"]
REGION_NAME = os.getenv('REGION_NAME', 'us-east-1')
dynamo = boto3.resource('dynamodb', region_name=REGION_NAME, endpoint_url="https://dynamodb.us-east-1.amazonaws.com")
# Define the URL of the targeted Slack API resource.
SLACK_URL = "https://slack.com/api/chat.postMessage"
def getAssetExistance(asset):
dynamoTable = dynamo.Table('Assets')
response = dynamoTable.query(KeyConditionExpression=Key('userID').eq(asset))
return bool(response)
def lambda_handler(data, context):
# Slack challenge answer.
if "challenge" in data:
return data["challenge"]
# Grab the Slack channel data.
slack_event = data['event']
slack_userID = slack_event["user"]
slack_text = slack_event["text"]
channel_id = slack_event["channel"]
slack_reply = ""
# Ignore bot messages.
if "bot_id" in slack_event:
slack_reply = ""
else:
# Start data sift.
if slack_text.startswith("!networth"):
slack_reply = "Your networth is: "
elif slack_text.startswith("!price"):
command,asset = text.split()
slack_reply = "The price of a(n) %s is: " % (asset)
elif slack_text.startswith("!addme"):
if not getAssetExistance(slack_userID):
slack_reply = "Adding user: %s" % (slack_userID)
dynamo.update_item(TableName=ASSET_TABLE,
Key={'userID':{'S':'slack_userID'}},
AttributeUpdates= {
'resources':{
'Action': 'ADD',
'Value': {'N': '1000'}
}
}
)
else:
slack_reply = "User %s already exists" % (slack_userID)
# We need to send back three pieces of information:
data = urllib.parse.urlencode(
(
("token", BOT_TOKEN),
("channel", channel_id),
("text", slack_reply)
)
)
data = data.encode("ascii")
# Construct the HTTP request that will be sent to the Slack API.
request = urllib.request.Request(
SLACK_URL,
data=data,
method="POST"
)
# Add a header mentioning that the text is URL-encoded.
request.add_header(
"Content-Type",
"application/x-www-form-urlencoded"
)
# Fire off the request!
urllib.request.urlopen(request).read()
# Everything went fine.
return "200 OK"
Run Code Online (Sandbox Code Playgroud)
我的DynamoDB表名为'Assets',并且有一个名为'userID'的主分区键(字符串).
我绝对还是新手,所以不要害怕称我为假人.任何和所有的帮助表示赞赏.此代码的目标是检查用户是否作为DynamoDB中的键存在,如果不存在,则将它们添加到表中.
Mar*_*k B 13
您需要导入该Key
函数,如下所示:
from boto3.dynamodb.conditions import Key
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2499 次 |
最近记录: |