Best Python code snippet using localstack_python
lambda_api.py
Source:lambda_api.py  
...329        zip_file_content = base64.b64decode(zip_file_content)330    else:331        return error_response('No valid Lambda archive specified.', 400)332    return zip_file_content333def get_java_handler(zip_file_content, handler, main_file):334    """Creates a Java handler from an uploaded ZIP or JAR.335    :type zip_file_content: bytes336    :param zip_file_content: ZIP file bytes.337    :type handler: str338    :param handler: The lambda handler path.339    :type main_file: str340    :param main_file: Filepath to the uploaded ZIP or JAR file.341    :returns: function or flask.Response342    """343    if not is_jar_archive(zip_file_content):344        with zipfile.ZipFile(BytesIO(zip_file_content)) as zip_ref:345            jar_entries = [e for e in zip_ref.infolist() if e.filename.endswith('.jar')]346            if len(jar_entries) != 1:347                raise Exception('Expected exactly one *.jar entry in zip file, found %s' % len(jar_entries))348            zip_file_content = zip_ref.read(jar_entries[0].filename)349            LOG.info('Found jar file %s with %s bytes in Lambda zip archive' %350                     (jar_entries[0].filename, len(zip_file_content)))351            main_file = new_tmp_file()352            save_file(main_file, zip_file_content)353    if is_jar_archive(zip_file_content):354        def execute(event, context):355            result, log_output = lambda_executors.EXECUTOR_LOCAL.execute_java_lambda(356                event, context, handler=handler, main_file=main_file)357            return result358        return execute359    return error_response(360        'Unable to extract Java Lambda handler - file is not a valid zip/jar files', 400, error_type='ValidationError')361def set_function_code(code, lambda_name):362    def generic_handler(event, context):363        raise Exception(('Unable to find executor for Lambda function "%s". ' +364            'Note that Node.js and .NET Core Lambdas currently require LAMBDA_EXECUTOR=docker') % lambda_name)365    lambda_cwd = None366    arn = func_arn(lambda_name)367    runtime = arn_to_lambda[arn].runtime368    handler_name = arn_to_lambda.get(arn).handler369    lambda_environment = arn_to_lambda.get(arn).envvars370    if not handler_name:371        handler_name = LAMBDA_DEFAULT_HANDLER372    # Stop/remove any containers that this arn uses.373    LAMBDA_EXECUTOR.cleanup(arn)374    # Save the zip file to a temporary file that the lambda executors can reference.375    zip_file_content = get_zip_bytes(code)376    if isinstance(zip_file_content, Response):377        return zip_file_content378    tmp_dir = '%s/zipfile.%s' % (config.TMP_FOLDER, short_uid())379    mkdir(tmp_dir)380    tmp_file = '%s/%s' % (tmp_dir, LAMBDA_ZIP_FILE_NAME)381    save_file(tmp_file, zip_file_content)382    TMP_FILES.append(tmp_dir)383    lambda_cwd = tmp_dir384    # Set the appropriate lambda handler.385    lambda_handler = generic_handler386    if runtime == LAMBDA_RUNTIME_JAVA8:387        # The Lambda executors for Docker subclass LambdaExecutorContainers,388        # which runs Lambda in Docker by passing all *.jar files in the function389        # working directory as part of the classpath. Because of this, we need to390        # save the zip_file_content as a .jar here.391        if is_jar_archive(zip_file_content):392            jar_tmp_file = '{working_dir}/{file_name}'.format(393                working_dir=tmp_dir, file_name=LAMBDA_JAR_FILE_NAME)394            save_file(jar_tmp_file, zip_file_content)395        lambda_handler = get_java_handler(zip_file_content, handler_name, tmp_file)396        if isinstance(lambda_handler, Response):397            return lambda_handler398    else:399        handler_file = get_handler_file_from_name(handler_name, runtime=runtime)400        handler_function = get_handler_function_from_name(handler_name, runtime=runtime)401        # Lambda code must be uploaded in the Zip format.402        if not is_zip_file(zip_file_content):403            raise Exception(404                'Uploaded Lambda code for runtime ({}) is not in Zip format'.format(runtime))405        unzip(tmp_file, tmp_dir)406        main_file = '%s/%s' % (tmp_dir, handler_file)407        if os.path.isfile(main_file):408            # make sure the file is actually readable, then read contents409            ensure_readable(main_file)...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
