我编写了一个跟踪登录AWS控制台的lambda代码,并向用户发送有关它的电子邮件通知。
我编写的初始代码是用Java编写的,并且有效。将代码转换为Scala后,我得到了以下代码:
class SNSHandler {
private val creds: AWSCredentials = new BasicAWSCredentials("xxx", "999/xyz12345")
private val eventType: String = "ConsoleLogin"
private val topicArn: String = "arn:aws:sns:us-east-1:1111111111:CTInterestingEvents"
def processLoginRecord(loginRecord: String, lambdaLogger: LambdaLogger) = {
val userName = JsonPath.read(loginRecord.asInstanceOf[Object], "$.userIdentity.type").asInstanceOf[String] match {
case "Root" => "Root"
case _ => JsonPath.read(loginRecord.asInstanceOf[Object], "$.userIdentity.userName")
}
val accountId = JsonPath.read(loginRecord.asInstanceOf[Object], "$.userIdentity.accountId")
new AmazonSNSClient(creds).publish(topicArn, "This is an auto notification message.\nUser " + userName +
" has logged in to AWS account id " + accountId + ".\n You are receiving this email because someone has subscribed your" +
" email address to this event.")
}
def processCloudTrailBulk(event: String, logger: LambdaLogger) = {
JsonPath.read(event.asInstanceOf[Object], "$.Records[?(@.eventName == '" + eventType + "' && @.responseElements.ConsoleLogin == 'Success')]").
asInstanceOf[java.util.List[String]].asScala.map(loginRecord => processLoginRecord(loginRecord, logger))
}
def processS3File(bucketName: String, file: String, logger: LambdaLogger) = {
Source.fromInputStream(new GZIPInputStream(new AmazonS3Client(creds).
getObject(new GetObjectRequest(bucketName, file)).getObjectContent),"UTF-8").getLines().
foreach(line => processCloudTrailBulk(line,logger))
}
def processSNSRecord(notification: SNSRecord, logger: LambdaLogger) = {
val bucketName: String = JsonPath.read(notification.getSNS.getMessage.asInstanceOf[Object], "$.s3Bucket")
logger.log("Notifications arrived.\nBucket: " + bucketName)
JsonPath.read(notification.getSNS.getMessage.asInstanceOf[Object], "$.s3ObjectKey[*]").asInstanceOf[java.util.List[String]].
asScala.map(file => processS3File(bucketName,file,logger))
}
def handleCloudTrailIncoming(event: SNSEvent, context: Context) = {
event.getRecords.asScala.map(record => processSNSRecord(record,context.getLogger))
}
}
现在,将.asInstanceOf [Object]添加到每个'read'调用的第一个参数中最初并不存在,但是我有着名的编译器错误,即对重载函数的模糊引用,并在看了之后: ambiguous reference to overloaded definition, from a Java library我添加了它,现在我的代码确实编译了。
然而问题是在运行时,读取现在无法检测到字段,我收到以下错误:
Property ['s3Bucket'] not found in path $: com.jayway.jsonpath.PathNotFoundException com.jayway.jsonpath.PathNotFoundException: Property ['s3Bucket'] not found in path $ at com.jayway.jsonpath.internal.token.PropertyPathToken.evaluate(PropertyPathToken.java:41) ........