我已经构建了一个.NET Core aws Lambda,它从S3读取xml文件并对其进行一些处理。当我在本地运行lambda时,它没有问题;但是,当我上传Lambda并尝试执行它时,它会暂停尝试获取文件的内容,并且cloudwatch中没有显示错误。这是我的代码有问题:
public async Task<string> GetFileContentsFromS3(string bucket, string key)
{
try
{
Console.WriteLine("Getting File Contents for Bucket: " + bucket + " and Key: " + key);
GetObjectRequest request = new GetObjectRequest
{
BucketName = bucket,
Key = key
};
Console.WriteLine("request: " + request.BucketName + " | " + request.Key);
using (GetObjectResponse responseObject = await S3Client.GetObjectAsync(request))
{
Console.WriteLine("In Response Object");
using (Stream responseStream = responseObject.ResponseStream)
{
Console.WriteLine("In Response Stream");
using (StreamReader reader = new StreamReader(responseStream))
{
Console.WriteLine("Successfully got File Contents");
return reader.ReadToEnd();
}
}
}
}
catch(Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
return string.Empty;
}
}
当我查看我的cloudwatch日志时,我可以看到此输出:
Console.WriteLine("Getting File Contents for Bucket: " + bucket + " and Key: " + key); and this: Console.WriteLine("request: " + request.BucketName + " | " + request.Key);
之后我没有得到任何东西,并且日志注意到功能在完成之前结束。它没有达到超时,因为设置为30秒并且我的内存分配应该足够了。另外,我已经让我的IAM用户完全控制了S3,因此不应该成为问题。我认为这就是这条线:
using (GetObjectResponse responseObject = await S3Client.GetObjectAsync(request))
这导致了问题,但我不知道另一种获取文件内容的方法或可能是实际问题。
答案 0 :(得分:0)
尝试使用属于ILambdaContext(入口点的第二个参数)的Logger,而不是使用Console.WriteLine。如果您的lambda配置了必要的权限,它将输出到CloudWatch。