首页 > 解决方案 > 加载 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 类

提前致谢

标签: javaaws-lambdaaws-documentdb

解决方案


就我而言,我将文件存储在资源文件夹中,以下工作:

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

推荐阅读