gpt4 book ai didi

java - 正在加载 rds-combined-ca-bundle.pem。来自AWS lambda java中的资源文件夹

转载 作者:行者123 更新时间:2023-11-30 05:43:17 24 4
gpt4 key购买 nike

我正在尝试加载“rds-combined-ca-bundle.pem”以从AWS Lambda连接DocumentDB,当从eclipse运行测试lambda时,它会获取该文件,因为该文件位于目录的根目录中。但是当上传jar时到 AWS 控制台,因为 Lamdba 函数文件位于资源 floder 并对其进行测试。获取 NULL 指针异常。

java.lang.NullPointerException
at com.adp.hyperx.documentdb.LambdaFunctionHandler.createCertificate(LambdaFunctionHandler.java:156)
at com.adp.hyperx.documentdb.LambdaFunctionHandler.createKeyStoreFile(LambdaFunctionHandler.java:144)
at com.adp.hyperx.documentdb.LambdaFunctionHandler.setSslProperties(LambdaFunctionHandler.java:131)
at com.adp.hyperx.documentdb.LambdaFunctionHandler.handleRequest(LambdaFunctionHandler.java:64)
at com.adp.hyperx.documentdb.LambdaFunctionHandler.handleRequest(LambdaFunctionHandler.java:46)
at lambdainternal.EventHandlerLoader$PojoHandlerAsStreamHandler.handleRequest(EventHandlerLoader.java:178)
at lambdainternal.EventHandlerLoader$2.call(EventHandlerLoader.java:888)
at lambdainternal.AWSLambda.startRuntime(AWSLambda.java:293)
at lambdainternal.AWSLambda.<clinit>(AWSLambda.java:64)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:348)




private static X509Certificate createCertificate() throws Exception
{
CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
// System.out.println(ClassLoader.class.getResourceAsStream("rds-combined-ca-bundle.pem"));
Path path = Paths.get(ClassLoader.class.getResource("/").toURI());
System.out.println("path" + path);
Path resourceLocation = path.resolve("rds-combined-ca-bundle.pem");
System.out.println("resourceLocation" + resourceLocation);
URL url = resourceLocation.toFile().toURI().toURL();
// URL url = new File(SSL_CERTIFICATE).toURI().toURL(); --this one reads the file from root floder
if (url == null) {
throw new Exception();
}
try (InputStream certInputStream = url.openStream()) {
// FileInputStream input = new FileInputStream(file);
return (X509Certificate)
certFactory.generateCertificate(certInputStream);
} catch (Exception e) {
e.printStackTrace();
return null;
}

}

我使用了与 RDS 相同的代码,但用于文档数据库 https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/UsingWithRDS.IAMDBAuth.Connecting.Java.html对于 IAMDatabaseAuthenticationTester 类

提前致谢

最佳答案

就我而言,我将文件存储在资源文件夹中,并且以下内容有效:

ClassLoader.getSystemClassLoader().getResource("rds-combined-ca-bundle.pem")

关于java - 正在加载 rds-combined-ca-bundle.pem。来自AWS lambda java中的资源文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55290288/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com