【发布时间】:2018-03-18 09:33:51
【问题描述】:
我是一般使用 AWS 和云服务的新手,但我正在尝试创建一个后端来处理从 DynamodDB 获取项目,如果它们不存在则创建它们。我试图通过使用 API 网关调用 lambda 并使用 lambda 处理数据库上的工作来完成此操作。我创建了一种示例 lambda,它扫描数据库并返回一个字符串。我在视觉工作室中使用 AWS lambda 项目完成了这项工作。在视觉工作室和 lambda 设计器上的测试事件中为其提供字符串的示例输入时,它可以正常工作并返回包含预期结果的字符串。所以我尝试添加一个新的 API 作为触发器,但我不知道如何配置它来发送正确的输入。我已经为此工作了几个小时,但找不到任何有关将数据作为参数发送到 lambda 的信息。当我尝试在浏览器中运行 api 时,我得到 {"message": "Internal server error"}。当我在 api 网关上对其进行测试时,我得到了这个。
Sat Mar 17 18:50:38 UTC 2018 : Endpoint response body before transformations: {
"errorType": "JsonReaderException",
"errorMessage": "Unexpected character encountered while parsing value: {.
Path '', line 1, position 1.",
"stackTrace": [
"at Newtonsoft.Json.JsonTextReader.ReadStringValue(ReadType readType)",
"at Newtonsoft.Json.JsonTextReader.ReadAsString()",
"at
Newtonsoft.Json.Serialization.JsonSerializerInternalReader.ReadForType
(JsonReader reader, JsonContract contract, Boolean hasConverter)",
"at
Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize
(JsonReader reader, Type objectType, Boolean checkAdditionalContent)",
"at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader,
Type objectType)",
"at Newtonsoft.Json.JsonSerializer.Deserialize[T](JsonReader reader)",
"at Amazon.Lambda.Serialization.Json.JsonSerializer.Deserialize[T](Stream
requestStream)",
"at lambda_method(Closure , Stream , Stream , LambdaContextInternal )"
]
}
Sat Mar 17 18:50:38 UTC 2018 : Endpoint response headers: {X-Amz-Executed-
Version=$LATEST, x-amzn-Remapped-Content-Length=0, Connection=keep-alive, x-
amzn-RequestId=15d89cfe-2a14-11e8-982c-db6e675f8b1d, Content-Length=939, X-
Amz-Function-Error=Unhandled, Date=Sat, 17 Mar 2018 18:50:38 GMT, X-Amzn-
Trace-Id=root=1-5aad637e-629010f757ae9b77679f6f40;sampled=0, Content-
Type=application/json}
Sat Mar 17 18:50:38 UTC 2018 : Execution failed due to configuration error:
Malformed Lambda proxy response
Sat Mar 17 18:50:38 UTC 2018 : Method completed with status: 502
下面是我的 lambda 的副本,我通过将其添加为触发器并将其设置为开放安全性来配置 API。我只是不明白如何将参数设置为目标输入。
TESTLAMBDA
[assembly:LambdaSerializer(typeof(Amazon.Lambda.Serialization.Json.JsonSerializer))]
namespace FindItem
{
public class Function
{
/// <summary>
/// A simple function that takes a string and does a ToUpper
/// </summary>
/// <param name="input"></param>
/// <param name="context"></param>
/// <returns></returns>
public string FunctionHandler(string input, ILambdaContext context)
{
AmazonDynamoDBClient client = GetClient();
Table table = GetTableObject(client, "Stores");
if (table == null)
{
PauseForDebugWindow();
return "Failure";
}
ScanFilter filter = new ScanFilter();
filter.AddCondition("Item_name", ScanOperator.Contains, new DynamoDBEntry[] { input });
ScanOperationConfig config = new ScanOperationConfig
{
AttributesToGet = new List<string> { "Store, Item_name, Aisle, Price" },
Filter = filter
};
Search search = table.Scan(filter);
List<Document> docList = new List<Document>();
Task<String> obj = traversedoc(docList,search);
return obj.Result;
}
/// /////////////////////////////////////////////////////////////////////////
public async Task<String> traversedoc(List<Document>docList,Search search)
{
string astring = "";
do
{
try
{
docList = await search.GetNextSetAsync();
}
catch (Exception ex)
{
Console.WriteLine("\n Error: Search.GetNextStep failed because: " + ex.Message);
break;
}
foreach (var doc in docList)
{
astring = astring + doc["Store"] + doc["Item_name"] + doc["Aisle"] + doc["Price"];
}
} while (!search.IsDone);
return astring;
}
public static AmazonDynamoDBClient GetClient()
{
// First, set up a DynamoDB client for DynamoDB Local
AmazonDynamoDBConfig ddbConfig = new AmazonDynamoDBConfig();
AmazonDynamoDBClient client;
try
{
client = new AmazonDynamoDBClient(ddbConfig);
}
catch (Exception ex)
{
Console.WriteLine("\n Error: failed to create a DynamoDB client; " + ex.Message);
return (null);
}
return (client);
}
public static Table GetTableObject(AmazonDynamoDBClient client, string tableName)
{
Table table = null;
try
{
table = Table.LoadTable(client, tableName);
}
catch (Exception ex)
{
Console.WriteLine("\n Error: failed to load the 'Movies' table; " + ex.Message);
return (null);
}
return (table);
}
public static void PauseForDebugWindow()
{
// Keep the console open if in Debug mode...
Console.Write("\n\n ...Press any key to continue");
Console.ReadKey();
Console.WriteLine();
}
}
}
【问题讨论】:
标签: c# amazon-web-services aws-lambda aws-api-gateway