在AWS上执行示例“ AWS Lambda for S3Event”代码时,找不到类错误

时间:2019-05-11 02:52:09

标签: apache amazon-web-services maven java-8 aws-lambda

  1. 我使用AWS Lambda模板创建了Eclipse项目。在AWS上执行完整的jar(具有所有依赖项)时,它将引发:
  

NoClassDefFoundError异常:
      org / apache / http / protocol / HttpRequestExecutor:
   java.lang.NoClassDefFoundError
      java.lang.NoClassDefFoundError:
   org / apache / http / protocol / HttpRequestExecutor
      在com.amazonaws.http.AmazonHttpClient。(AmazonHttpClient.java:156)
      访问com.amazonaws.AmazonWebServiceClient。 (AmazonWebServiceClient.java:226)
      访问com.amazonaws.AmazonWebServiceClient。 (AmazonWebServiceClient.java:215)
      在com.amazonaws.services.s3.AmazonS3Client。(AmazonS3Client.java:682)
      在com.amazonaws.services.s3.AmazonS3Builder $ 1.apply(AmazonS3Builder.java:35)
      在com.amazonaws.services.s3.AmazonS3Builder $ 1.apply(AmazonS3Builder.java:32)
      在com.amazonaws.services.s3.AmazonS3ClientBuilder.build(AmazonS3ClientBuilder.java:64)
      在com.amazonaws.services.s3.AmazonS3ClientBuilder.build(AmazonS3ClientBuilder.java:28)
      在com.amazonaws.client.builder.AwsSyncClientBuilder.build(AwsSyncClientBuilder.java:46

  1. 请注意,它找不到课程:org/apache/http/protocol/HttpRequestExecutor apache http类很多,尽管重建了,但上面的类似乎丢失了。 POM如下所示。 知道为什么可能缺少该特定班级吗?

  2. 瓶子内容:

$ jar tvf target/demo-1.0.0.jar | grep -i HttpRequestE
  2545 Fri May 10 21:12:24 EDT 2019 com/amazonaws/http/protocol/SdkHttpRequestExecutor.class
$ jar tvf target/demo-1.0.0.jar | grep -i protocol | grep http
     0 Fri May 10 21:12:24 EDT 2019 com/amazonaws/http/protocol/
  2545 Fri May 10 21:12:24 EDT 2019 com/amazonaws/http/protocol/SdkHttpRequestExecutor.class
  1423 Fri May 10 21:12:24 EDT 2019 com/amazonaws/http/conn/ssl/TLSProtocol.class
     0 Fri May 10 21:12:24 EDT 2019 org/apache/http/client/protocol/
  2667 Fri May 10 21:12:24 EDT 2019 org/apache/http/client/protocol/RequestClientConnControl.class
   852 Fri May 10 21:12:24 EDT 2019 org/apache/http/client/protocol/ResponseAuthCache$1.class
  7352 Fri May 10 21:12:24 EDT 2019 org/apache/http/client/protocol/RequestAddCookies.class
  5001 Fri May 10 21:12:24 EDT 2019 org/apache/http/client/protocol/RequestAuthCache.class
  1814 Fri May 10 21:12:24 EDT 2019 org/apache/http/client/protocol/ClientContextConfigurer.class
  2485 Fri May 10 21:12:24 EDT 2019 org/apache/http/client/protocol/RequestExpectContinue.class
  5071 Fri May 10 21:12:24 EDT 2019 org/apache/http/client/protocol/ResponseContentEncoding.class
  4521 Fri May 10 21:12:24 EDT 2019 org/apache/http/client/protocol/RequestAuthenticationBase.class
  6520 Fri May 10 21:12:24 EDT 2019 org/apache/http/client/protocol/HttpClientContext.class
  5647 Fri May 10 21:12:24 EDT 2019 org/apache/http/client/protocol/ResponseProcessCookies.class
  1190 Fri May 10 21:12:24 EDT 2019 org/apache/http/client/protocol/ClientContext.class
  5053 Fri May 10 21:12:24 EDT 2019 org/apache/http/client/protocol/ResponseAuthCache.class
  2742 Fri May 10 21:12:24 EDT 2019 org/apache/http/client/protocol/RequestProxyAuthentication.class
  7009 Fri May 10 21:12:24 EDT 2019 org/apache/http/impl/execchain/ProtocolExec.class
   910 Fri May 10 21:12:24 EDT 2019 org/apache/http/client/ClientProtocolException.class
  1219 Fri May 10 21:12:24 EDT 2019 org/apache/http/auth/AuthProtocolState.class
  2523 Fri May 10 21:12:24 EDT 2019 org/apache/http/client/protocol/RequestAcceptEncoding.class
  2465 Fri May 10 21:12:24 EDT 2019 org/apache/http/client/protocol/RequestDefaultHeaders.class
   934 Fri May 10 21:12:24 EDT 2019 org/apache/http/client/protocol/RequestAuthenticationBase$1.class
  2544 Fri May 10 21:12:24 EDT 2019 org/apache/http/client/protocol/RequestTargetAuthentication.class
  1. POM
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>

      <groupId>test.lambda</groupId>
      <artifactId>demo</artifactId>
      <version>1.0.0</version>
      <packaging>jar</packaging>

      <dependencyManagement>
        <dependencies>
          <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-java-sdk-bom</artifactId>
            <version>1.11.550</version>
            <type>pom</type>
            <scope>import</scope>
          </dependency>
        </dependencies>
      </dependencyManagement>

      <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>4.12</version>
          <scope>test</scope>
        </dependency>
        <dependency>
          <groupId>org.mockito</groupId>
          <artifactId>mockito-core</artifactId>
          <version>2.7.22</version>
          <scope>test</scope>
        </dependency>

        <dependency>
          <groupId>com.amazonaws</groupId>
          <artifactId>aws-java-sdk-s3</artifactId>
        </dependency>
        <dependency>
          <groupId>com.amazonaws</groupId>
          <artifactId>aws-lambda-java-events</artifactId>
          <version>1.3.0</version>
        </dependency>
        <dependency>
          <groupId>com.amazonaws</groupId>
          <artifactId>aws-lambda-java-core</artifactId>
          <version>1.1.0</version>
        </dependency>
      </dependencies>

      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.6.0</version>
            <configuration>
              <source>1.8</source>
              <target>1.8</target>
              <encoding>UTF-8</encoding>
              <forceJavacCompilerUse>true</forceJavacCompilerUse>
            </configuration>
          </plugin>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>3.0.0</version>
            <executions>
              <execution>
                <phase>package</phase>
                <goals>
                  <goal>shade</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    </project>
  1. 代码
    public class LambdaFunctionHandler implements RequestHandler<S3Event, String> {

        private AmazonS3 s3 = AmazonS3ClientBuilder.standard().build();

        public LambdaFunctionHandler() {}

        @Override
        public String handleRequest(S3Event event, Context context) {
            context.getLogger().log("Received event: " + event);

            // Get the object from the event and show its content type
            String bucket = event.getRecords().get(0).getS3().getBucket().getName();
            String key = event.getRecords().get(0).getS3().getObject().getKey();
            try {
                S3Object response = s3.getObject(new GetObjectRequest(bucket, key));
                String contentType = response.getObjectMetadata().getContentType();
                context.getLogger().log("CONTENT TYPE: " + contentType);
                return contentType;
            } catch (Exception e) {
                e.printStackTrace();
                context.getLogger().log(String.format(
                    "Error getting object %s from bucket %s. Make sure they exist and"
                    + " your bucket is in the same region as this function.", key, bucket));
                throw e;
            }
        }
    }

0 个答案:

没有答案