为什么Java中的AWS Lambda函数返回空白JSON

时间:2017-04-23 13:11:21

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

我无法理解为什么我的AWS Lambda函数返回{}即空白JSON,即使有响应获取打印CloudWatch日志。

这是我的代码。从S3获取详细信息。在日志中,我可以在日志中看到这些细节。但是当我测试lambda函数时,我得到{}而不是实际的响应。知道我在这里做错了吗?

我在这里注意到的另一件事。当我将返回类型更改为String而不是JSONObject时,我得到了预期的JSON字符串。奇怪。不是吗?

    public class GetItemDetailsHandler implements RequestHandler<Object, JSONObject>{

    static AmazonS3 s3Client;

    static {
        s3Client = new AmazonS3Client(new BasicAWSCredentials(ACCESS_KEY, SECRET_KEY));
    }

    public JSONObject handleRequest(Object obj, Context context) {

        String key = "somePath";

        S3Object s3object = s3Client.getObject(BUCKET_NAME, key);
        context.getLogger().log("s3object content = "+ s3object.getObjectContent());
        context.getLogger().log("s3object metatdata = "+ s3object.getObjectMetadata());

        JSONObject itemDetails = null;
        try {
            itemDetails = getJSONObjectFromS3Object(s3object.getObjectContent(), context);
        } catch (IOException e) {
            context.getLogger().log("Exception while parsing JSON. Exception =>" + e.getMessage());
        }
        context.getLogger().log("itemDetails = "+ itemDetails);
        context.getLogger().log("itemDetails class = "+ itemDetails.getClass());

        return itemDetails;
    }

     private static JSONObject getJSONObjectFromS3Object(InputStream is, Context context) throws IOException {
        if (is == null)
            return null;

        StringBuilder sb = new StringBuilder();
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(is, StringUtils.UTF8));
            String line;
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
        } finally {
            is.close();
        }

        context.getLogger().log("JSON = "+ sb.toString());
        return new JSONObject(sb.toString());
    }
}

0 个答案:

没有答案