调用lambda函数时执行失败

时间:2017-08-04 07:44:25

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

我正在尝试调用由S3Event触发的lambda函数,我也创建了一个存储桶,还在存储桶中添加了两个图像。

以下是桶的规格。

enter image description here

下面是我用java编写的代码

  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.", bucket, key));
        throw e;
    }
}

以下是我得到的错误

com.amazonaws.services.lambda.runtime.events.S3Event不存在

1 个答案:

答案 0 :(得分:1)

代码看起来很好,请确认您已导入此软件包:

com.amazonaws.services.lambda.runtime.events.S3Event

并与您的班级实现“RequestHandler”接口。

如果问题仍然存在,请按照本教程进行操作: AWS Lambda with S3 for real-time data processing

希望这会有所帮助!