【发布时间】:2017-01-22 03:25:33
【问题描述】:
我正在使用 AWS Lambda 函数来使用 Boto3 创建 API 密钥。
用以下方法在本地测试成功:
import boto3
client = boto3.client('apigateway')
response = client.create_api_key(
name='test_user_from_boto',
description='This is the description',
enabled=True,
generateDistinctId=True,
value='',
stageKeys=[{
'restApiId':'aaa',
'stageName':'beta'
}]
)
将字典返回为expected 没有问题。返回字典包含一个 value 键,它具有生成的 api 键值,这就是我所追求的。
在 AWS Lambda 中执行类似操作时,返回字典不包含 value 键。
这是我的 Lambda 处理函数。
import boto3
api_id = 'zzz'
plan_id_map = {
'trial': 'aaa',
'basic': 'bbb',
'professional': 'ccc'
}
def handler(event, context):
user_name = event['user_name']
stage = event['stage']
plan = event['plan']
client = boto3.client('apigateway')
api_key_response = client.create_api_key(
name=user_name,
description='',
enabled=True,
# generateDistinctId=True, # including this argument throws an error
# value='', # including this argument throws an error
stageKeys=[{
'restApiId': api_id,
'stageName': stage
}]
)
user_key_id = api_key_response['id']
user_api_key = api_key_response['value'] # throws a key error here
plan_response = client.create_usage_plan_key(
usagePlanId=plan_id_map[plan],
keyId=user_key_id,
keyType='API_KEY')
return {
'user_name': user_name,
'user_key_id': user_key_id,
'user_api_key': user_api_key
}
打印api_key_response的结果如下:
{
u'name': u'test_user_from_lambda',
'ResponseMetadata': {
'HTTPStatusCode': 201,
'RequestId': 'b8298d38-7aec-11e6-8322-5bc341fc4b73',
'HTTPHeaders': {
'x-amzn-requestid': 'b8298d38-7aec-11e6-8322-5bc341fc4b73',
'date': 'Thu, 15 Sep 2016 02:33:00 GMT',
'content-length': '203',
'content-type': 'application/json'
}
},
u'createdDate': datetime.datetime(2016, 9, 15, 2, 33, tzinfo=tzlocal()),
u'lastUpdatedDate': datetime.datetime(2016, 9, 15, 2, 33, tzinfo=tzlocal()),
u'enabled': True,
u'id': u'xyzxyz',
u'stageKeys': [u'abcabc/beta']
}
尝试使用get_api_key 时,出现参数验证错误:
get_api_key_response = client.get_api_key(
apiKey='585yw0f1tk',
includeValue=True
)
Unknown parameter in input: "includeValue", must be one of: apiKey: ParamValidationError
是否修改了 AWSboto3 模块以排除 value 键?生成的api key如何返回?
【问题讨论】:
-
它会抛出什么错误?
-
这不是错误,键
value只是在 AWS Lambda 实现的字典中返回,否则它会返回。 -
那么
api_key_response在那个时候是什么样子的?尝试打印它 -
用结果更新了 OP
标签: python amazon-web-services aws-lambda aws-api-gateway boto3