AWS Lambda Java到S3 - com.amazonaws.services.s3.AmazonS3ClientBuilder上的java.lang.ClassNotFoundException

时间:2018-02-28 01:32:45

标签: java amazon-web-services amazon-s3 aws-lambda aws-sdk

我尝试在本地运行以下代码(作为Intellij中的maven项目),它运行正常。当你尝试将它作为Lambda函数运行时,我总是得到一个java.lang.NoClassDefFoundError不知怎的,lambda使它在下面包含AmazonS3行时找不到类。

package example;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
public class ParserLambda implements RequestHandler<Object, Object> {
        @Override
        public Object handleRequest(Object input, Context context) {
            String retval = "";
            try {
                AmazonS3 s3Client = AmazonS3ClientBuilder.standard().build();
            } catch (Exception ex) {
                return "{'status':'error', 'retval': '" + ex.getMessage() + "'}";
            }
            return "{'status':'done', 'retval': '" + retval + "'}";
        }
}

如果你注释掉这一行    // AmazonS3 s3Client = AmazonS3ClientBuilder.standard()。build(); 它给出了:&#34; {&#39;状态&#39;:&#39;完成&#39;,&#39; retval&#39;:&#39;&#39;}&#34;这是一个有效的结果。

取消评论   AmazonS3 s3Client = AmazonS3ClientBuilder.standard()。build(); 你得到:

{
  "errorMessage": "com/amazonaws/services/s3/AmazonS3ClientBuilder",
  "errorType": "java.lang.NoClassDefFoundError",
  "stackTrace": [
    "example.ParserLambda.handleRequest(ParserLambda.java:11)",
    "sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)",
    "sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)",
    "sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)",
    "java.lang.reflect.Method.invoke(Method.java:498)"
  ],
  "cause": {
    "errorMessage": "com.amazonaws.services.s3.AmazonS3ClientBuilder",
    "errorType": "java.lang.ClassNotFoundException",
    "stackTrace": [
      "java.net.URLClassLoader.findClass(URLClassLoader.java:381)",
      "java.lang.ClassLoader.loadClass(ClassLoader.java:424)",
      "java.lang.ClassLoader.loadClass(ClassLoader.java:357)",
      "example.ParserLambda.handleRequest(ParserLambda.java:11)",
      "sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)",
      "sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)",
      "sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)",
      "java.lang.reflect.Method.invoke(Method.java:498)"
    ]
  }
}

我的pom文件包括:

       <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-java-sdk</artifactId>
            <version>1.11.286</version>
        </dependency>

我也尝试过:             AmazonS3 s3Client = AmazonS3ClientBuilder.defaultClient();             AmazonS3 s3Client = AmazonS3ClientBuilder.standard()。withRegion(Regions.US_EAST_1).withForceGlobalBucketAccessEnabled(true).build();

2 个答案:

答案 0 :(得分:0)

我认为问题是aws-java-sdk-s3没有作为aws-java-sdk的一部分提供给lambda函数,所以你必须明确地将它包含在你的.jar中。

我的pom看起来像这样清除错误:

boost::asio::spawn(my_strand, do_echo);

// ...

void do_echo(boost::asio::yield_context yield)
{
  try
  {
    char data[128];
    for (;;)
    {
      std::size_t length =
        my_socket.async_read_some(
          boost::asio::buffer(data), yield);

      boost::asio::async_write(my_socket,
          boost::asio::buffer(data, length), yield);
    }
  }
  catch (std::exception& e)
  {
    // ...
  }
}

答案 1 :(得分:0)

我遇到了相同的问题,经过几次尝试后都解决了。

在我的pom.xml中调整以下依赖项是不够的(注意<scope>provided</scope>依赖项的aws-java-sdk):

<!-- https://mvnrepository.com/artifact/com.amazonaws/aws-java-sdk -->
<dependency>
    <groupId>com.amazonaws</groupId>
    <artifactId>aws-java-sdk</artifactId>
    <version>1.11.664</version>
    <scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.amazonaws/aws-java-sdk-s3 -->
<dependency>
    <groupId>com.amazonaws</groupId>
    <artifactId>aws-java-sdk-s3</artifactId>
    <version>1.11.664</version>
</dependency>

结果是我在pom.xml中缺少以下插件:

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-shade-plugin</artifactId>
      <version>3.2.1</version>
      <configuration>
        <createDependencyReducedPom>false</createDependencyReducedPom>
      </configuration>
      <executions>
        <execution>
          <phase>package</phase>
          <goals>
            <goal>shade</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

然后我以错误的方式构建了jar文件。最好的方法是构建jar文件,方法是使用终端或命令提示符浏览到项目目录并执行以下命令:

mvn package

这包括jar文件中所有必需的依赖项。您的jar文件将位于./target目录中。


来源:https://docs.aws.amazon.com/lambda/latest/dg/java-create-jar-pkg-maven-no-ide.html