java - 加载 rds-combined-ca-bundle.pem。来自 AWS lambda java 中的资源文件夹
问题描述
我正在尝试加载“rds-combined-ca-bundle.pem”以从 AWS Lambda 连接 DocumentDB,它在从 eclipse 运行测试 lambda 时获取文件,因为该文件位于目录的根目录。但是当将 jar 上传到 AWS 控制台时因为 lamdba 函数文件位于资源库并对其进行测试。获取 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")
推荐阅读
- octobercms - 如何更改rainlab.user::mail.invite 以获得激活链接
- python-3.x - Jupyter 的选项卡自动完成功能不适用于 sklearn、numpy、seaborn、pandas 和 matplotlib 等多个库
- keras - 在 Keras 中,在我自己的模型上使用 include_top= False 会删除所有最后一个密集层,我可以定义模型的“顶部”吗?
- asp.net - 无法让 ASP.NET/IIS 使用自定义缓存控制设置来提供 .js 文件
- laravel - Laravel 网站域名将转到 public/publc url
- django - django 具有相同外键的多个模型
- python - 使用 psycopg2 在 PostgreSQL 中使用密码创建用户时出错
- sql - 日期过滤器返回无效字符错误
- javascript - 为什么在这个 POST 请求中,then() 处理函数的参数用大括号括起来?
- c# - XML反序列化不同对象C#