Search in sources :

Example 41 with LambdaLogger

use of com.amazonaws.services.lambda.runtime.LambdaLogger in project aws-doc-sdk-examples by awsdocs.

the class Handler method handleRequest.

@Override
public String handleRequest(Map<String, String> event, Context context) {
    LambdaLogger logger = context.getLogger();
    Gson gson = new GsonBuilder().create();
    // log execution details
    logger.log("ENVIRONMENT VARIABLES: " + gson.toJson(System.getenv()));
    logger.log("CONTEXT: " + gson.toJson(context));
    // process event
    logger.log("EVENT Data: " + gson.toJson(event));
    String myCaseID = event.get("inputCaseID");
    logger.log("CaseId44: " + myCaseID);
    return myCaseID;
}
Also used : GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson) LambdaLogger(com.amazonaws.services.lambda.runtime.LambdaLogger)

Example 42 with LambdaLogger

use of com.amazonaws.services.lambda.runtime.LambdaLogger in project aws-doc-sdk-examples by awsdocs.

the class DocumentHandler method handleRequest.

public String handleRequest(Map<String, String> event, Context context) throws IOException, BiffException {
    LambdaLogger logger = context.getLogger();
    logger.log("Getting excel doc from the Amazon S3 bucket");
    // Get the Amazon S3 bucket name and MS Excel file name.
    String bucketName = event.get("bucketname");
    String object = event.get("objectname");
    // Get the XML that contains the Pop data.
    ExcelService excel = new ExcelService();
    String xml = excel.getData(bucketName, object);
    return xml;
}
Also used : LambdaLogger(com.amazonaws.services.lambda.runtime.LambdaLogger)

Example 43 with LambdaLogger

use of com.amazonaws.services.lambda.runtime.LambdaLogger in project djl-demo by deepjavalibrary.

the class Handler method handleRequest.

@Override
public void handleRequest(InputStream is, OutputStream os, Context context) throws IOException {
    LambdaLogger logger = context.getLogger();
    String input = Utils.toString(is);
    try {
        Request request = GSON.fromJson(input, Request.class);
        String base64Img = request.getImageData().split(",")[1];
        byte[] imgBytes = Base64.getDecoder().decode(base64Img);
        Image img;
        try (ByteArrayInputStream bis = new ByteArrayInputStream(imgBytes)) {
            ImageFactory factory = ImageFactory.getInstance();
            img = factory.fromInputStream(bis);
        }
        Translator<Image, Classifications> translator = ImageClassificationTranslator.builder().addTransform(new ToTensor()).optFlag(Image.Flag.GRAYSCALE).optApplySoftmax(true).build();
        Criteria<Image, Classifications> criteria = Criteria.builder().setTypes(Image.class, Classifications.class).optModelUrls("https://djl-ai.s3.amazonaws.com/resources/demo/pytorch/doodle_mobilenet.zip").optTranslator(translator).build();
        ZooModel<Image, Classifications> model = ModelZoo.loadModel(criteria);
        try (Predictor<Image, Classifications> predictor = model.newPredictor()) {
            List<Classifications.Classification> result = predictor.predict(img).topK(5);
            os.write(GSON.toJson(result).getBytes(StandardCharsets.UTF_8));
        }
    } catch (RuntimeException | ModelException | TranslateException e) {
        logger.log("Failed handle input: " + input);
        logger.log(e.toString());
        String msg = "{\"status\": \"invoke failed: " + e.toString() + "\"}";
        os.write(msg.getBytes(StandardCharsets.UTF_8));
    }
}
Also used : Classifications(ai.djl.modality.Classifications) ToTensor(ai.djl.modality.cv.transform.ToTensor) ModelException(ai.djl.ModelException) TranslateException(ai.djl.translate.TranslateException) Image(ai.djl.modality.cv.Image) ImageFactory(ai.djl.modality.cv.ImageFactory) ByteArrayInputStream(java.io.ByteArrayInputStream) LambdaLogger(com.amazonaws.services.lambda.runtime.LambdaLogger)

Example 44 with LambdaLogger

use of com.amazonaws.services.lambda.runtime.LambdaLogger in project amazon-neptune-tools by awslabs.

the class RunNeptuneExportSvc method run.

@Override
public void run() {
    InputStream input = new ByteArrayInputStream(json.getBytes(StandardCharsets.UTF_8));
    try {
        new NeptuneExportLambda(rootPath, cleanRootPath, maxFileDescriptorCount).handleRequest(input, System.out, new Context() {

            @Override
            public String getAwsRequestId() {
                throw new NotImplementedException();
            }

            @Override
            public String getLogGroupName() {
                throw new NotImplementedException();
            }

            @Override
            public String getLogStreamName() {
                throw new NotImplementedException();
            }

            @Override
            public String getFunctionName() {
                throw new NotImplementedException();
            }

            @Override
            public String getFunctionVersion() {
                throw new NotImplementedException();
            }

            @Override
            public String getInvokedFunctionArn() {
                throw new NotImplementedException();
            }

            @Override
            public CognitoIdentity getIdentity() {
                throw new NotImplementedException();
            }

            @Override
            public ClientContext getClientContext() {
                throw new NotImplementedException();
            }

            @Override
            public int getRemainingTimeInMillis() {
                throw new NotImplementedException();
            }

            @Override
            public int getMemoryLimitInMB() {
                throw new NotImplementedException();
            }

            @Override
            public LambdaLogger getLogger() {
                return new LambdaLogger() {

                    @Override
                    public void log(String s) {
                        System.out.println(s);
                    }

                    @Override
                    public void log(byte[] bytes) {
                        throw new NotImplementedException();
                    }
                };
            }
        });
    } catch (Exception e) {
        e.printStackTrace();
        System.err.println("An error occurred while exporting from Neptune: " + e.getMessage());
        System.exit(-1);
    }
    System.exit(0);
}
Also used : Context(com.amazonaws.services.lambda.runtime.Context) ClientContext(com.amazonaws.services.lambda.runtime.ClientContext) NeptuneExportLambda(com.amazonaws.services.neptune.export.NeptuneExportLambda) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) NotImplementedException(sun.reflect.generics.reflectiveObjects.NotImplementedException) ClientContext(com.amazonaws.services.lambda.runtime.ClientContext) CognitoIdentity(com.amazonaws.services.lambda.runtime.CognitoIdentity) LambdaLogger(com.amazonaws.services.lambda.runtime.LambdaLogger) NotImplementedException(sun.reflect.generics.reflectiveObjects.NotImplementedException)

Example 45 with LambdaLogger

use of com.amazonaws.services.lambda.runtime.LambdaLogger in project amazon-neptune-tools by awslabs.

the class NeptuneEndpointsInfoLambda method handleRequest.

@Override
public void handleRequest(InputStream input, OutputStream output, Context context) throws IOException {
    LambdaLogger logger = context.getLogger();
    EndpointsType endpointsType = EndpointsType.ReadReplicas;
    Scanner scanner = new Scanner(input);
    if (scanner.hasNext()) {
        String param = scanner.next().replace("\"", "");
        if (!param.isEmpty()) {
            endpointsType = EndpointsType.valueOf(param);
        }
    }
    logger.log("EndpointsType: " + endpointsType);
    Map<EndpointsSelector, Collection<String>> addressesMap = addresses.get();
    for (Map.Entry<EndpointsSelector, Collection<String>> entry : addressesMap.entrySet()) {
        logger.log(entry.getKey() + ": " + entry.getValue());
    }
    String results = String.join(",", addressesMap.get(endpointsType));
    logger.log("Results: " + results);
    try (Writer writer = new BufferedWriter(new OutputStreamWriter(output, UTF_8))) {
        writer.write(results);
        writer.flush();
    }
}
Also used : Scanner(java.util.Scanner) EndpointsSelector(software.amazon.neptune.cluster.EndpointsSelector) EndpointsType(software.amazon.neptune.cluster.EndpointsType) Collection(java.util.Collection) LambdaLogger(com.amazonaws.services.lambda.runtime.LambdaLogger) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

LambdaLogger (com.amazonaws.services.lambda.runtime.LambdaLogger)59 HashMap (java.util.HashMap)10 Date (java.util.Date)9 List (java.util.List)8 Map (java.util.Map)8 Context (com.amazonaws.services.lambda.runtime.Context)6 ApiAuthorizer (com.formkiq.lambda.apigateway.ApiAuthorizer)6 ApiGatewayRequestEvent (com.formkiq.lambda.apigateway.ApiGatewayRequestEvent)6 ApiGatewayRequestEventUtil (com.formkiq.lambda.apigateway.ApiGatewayRequestEventUtil)6 ApiGatewayRequestHandler (com.formkiq.lambda.apigateway.ApiGatewayRequestHandler)6 ApiRequestHandlerResponse (com.formkiq.lambda.apigateway.ApiRequestHandlerResponse)6 IOException (java.io.IOException)6 SC_OK (com.formkiq.lambda.apigateway.ApiResponseStatus.SC_OK)5 AwsServiceCache (com.formkiq.lambda.apigateway.AwsServiceCache)5 BadException (com.formkiq.lambda.apigateway.exception.BadException)5 DynamicObject (com.formkiq.stacks.common.objects.DynamicObject)5 Collectors (java.util.stream.Collectors)5 ClientContext (com.amazonaws.services.lambda.runtime.ClientContext)4 CognitoIdentity (com.amazonaws.services.lambda.runtime.CognitoIdentity)4 ApiMapResponse (com.formkiq.lambda.apigateway.ApiMapResponse)4