我正在实现一个第三方应用程序,以使用AWS开发工具包简单地处理AWS Lambda的所有功能。我正在使用JetBrains IntelliJ Idea IDE进行编码。有人可以解释如何在Java中创建lambda对象以创建/调用aws lambda函数吗?
我提到了documentation,但是它没有提供创建lambda对象的步骤。
答案 0 :(得分:0)
您不必创建lambda对象。 Lambda函数在不使用时被冻结。这就是为什么全局变量不会丢失数据的原因。您只需要发送一个调用调用,AWS就会处理其余的事情。
我认为以上信息很高兴知道。现在,您可以在此link
中找到一个示例来调用lambda函数答案 1 :(得分:-1)
尝试这个课程。
import com.amazonaws.ClientConfiguration;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.regions.Region;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.lambda.AWSLambdaClient;
import com.amazonaws.services.lambda.model.InvocationType;
import com.amazonaws.services.lambda.model.InvokeRequest;
import com.amazonaws.services.lambda.model.InvokeResult;
import java.nio.charset.Charset;
public class AWSLambdaCaller {
private String accessKey;
private String secretKey;
private String region;
private int maxConnections;
private String functionName;
public AWSLambdaCaller() {
}
/* setter methods */
public void setAccessKey(String pAccessKey) {
accessKey = pAccessKey;
}
public void setSecretKey(String pSecretKey) {
secretKey = pSecretKey;
}
public void setRegion(String pRegion) {
region = pRegion;
}
public void setMaxConnections(int pMaxConnections) {
maxConnections = pMaxConnections;
}
public void setFunctionName(String pFunctionName) {
functionName = pFunctionName;
}
/* getter methods */
public String getAccessKey () {
return accessKey;
}
public String getSecretKey () {
return secretKey;
}
public String getRegion () {
return region;
}
public int getMaxConnections() {
return maxConnections;
}
public String getFunctionName () {
return functionName;
}
public String call() {
AWSCredentials awsCredentials = new BasicAWSCredentials(accessKey, secretKey);
ClientConfiguration clientConfiguration = new ClientConfiguration();
clientConfiguration.setMaxConnections(maxConnections);
AWSLambdaClient awsLambdaClient = new AWSLambdaClient(awsCredentials, clientConfiguration);
awsLambdaClient.setRegion(Region.getRegion(Regions.fromName(region)));
InvokeRequest invokeRequest = new InvokeRequest();
invokeRequest.setFunctionName(functionName);
invokeRequest.setInvocationType(InvocationType.RequestResponse);
// invokeRequest.setPayload(jsonBody.toString());
InvokeResult invokeResult = awsLambdaClient.invoke(invokeRequest);
String str = new String(invokeResult.getPayload().array(), Charset.forName("UTF-8"));
return str;
}
}
main()将会是这样。
AWSLambdaCaller awsLambdaCaller = new AWSLambdaCaller();
awsLambdaCaller.setAccessKey("Your access key");
awsLambdaCaller.setSecretKey("Your secret key");
awsLambdaCaller.setRegion("region");
awsLambdaCaller.setMaxConnections(1100);
awsLambdaCaller.setFunctionName("Your function name");
System.out.println("Calling AWS Lambda function...");
System.out.println("AWS>> " + awsLambdaCaller.call());
请注意,上述代码仅支持不带参数的lambda函数。要传递参数,请编辑invokeRequest。