import boto3
import os
import gzip
import shutil

def initialize_s3_client():
    """
    Initializes the S3 CLient. If running in Lambda mode, only the AWS REGION environment variable is needed.
    If not running in Lambda mode, the AWS credentials are also needed.
    """
    if(os.environ.get('MODE') == 'lambda'):
        s3Client = boto3.client('s3', region_name = os.environ.get('AWS_REGION'))
    else:
        s3Client = boto3.client('s3',
            aws_access_key_id = os.environ.get('AWS_ACCESS_KEY_ID'),
            aws_secret_access_key = os.environ.get('AWS_SECRET_ACCESS_KEY'), 
            region_name = os.environ.get('AWS_REGION')
        )
    return s3Client

def get_requests_from_file(s3Client, bucket, key):
    """
    Downloads the AWS ALB log file saved in S3, and input the logs into a list
    """
    try:
        print('Downloading log file from bucket {}'.format(bucket))
        # Download the file from s3
        s3Client.download_file(bucket, key, "/tmp/requests.gz")
        # Unzip the file and read the content
        with gzip.open('/tmp/requests.gz', 'rb') as zippedFile:
            with open('/tmp/requests.txt', 'wb') as textFile:
                shutil.copyfileobj(zippedFile, textFile)
        # Load the requests found in the file into a list
        with open('/tmp/requests.txt') as requestFile:
            listOfRequests = [line.rstrip('\n') for line in requestFile]
        os.remove('/tmp/requests.gz')
        os.remove('/tmp/requests.txt')
    except Exception as error:
        # Generate an error and exit the script
        print('Error while Downloading the log file from S3')
        raise Exception(error)
    print('Successfully downloaded the log file. It contains {} requests to be analyzed'.format(len(listOfRequests)))
    return listOfRequests
import boto3
import os
import gzip
import shutil

def initialize_s3_client():
    """
    Initializes the S3 CLient. If running in Lambda mode, only the AWS REGION environment variable is needed.
    If not running in Lambda mode, the AWS credentials are also needed.
    """
    if(os.environ.get('MODE') == 'lambda'):
        s3Client = boto3.client('s3', region_name = os.environ.get('AWS_REGION'))
    else:
        s3Client = boto3.client('s3',
            aws_access_key_id = os.environ.get('AWS_ACCESS_KEY_ID'),
            aws_secret_access_key = os.environ.get('AWS_SECRET_ACCESS_KEY'), 
            region_name = os.environ.get('AWS_REGION')
        )
    return s3Client

def get_requests_from_file(s3Client, bucket, key):
    """
    Downloads the AWS ALB log file saved in S3, and input the logs into a list
    """
    try:
        print('Downloading log file from bucket {}'.format(bucket))
        # Download the file from s3
        s3Client.download_file(bucket, key, "/tmp/requests.gz")
        # Unzip the file and read the content
        with gzip.open('/tmp/requests.gz', 'rb') as zippedFile:
            with open('/tmp/requests.txt', 'wb') as textFile:
                shutil.copyfileobj(zippedFile, textFile)
        # Load the requests found in the file into a list
        with open('/tmp/requests.txt') as requestFile:
            listOfRequests = [line.rstrip('\n') for line in requestFile]
        os.remove('/tmp/requests.gz')
        os.remove('/tmp/requests.txt')
    except Exception as error:
        # Generate an error and exit the script
        print('Error while Downloading the log file from S3')
        raise Exception(error)
    print('Successfully downloaded the log file. It contains {} requests to be analyzed'.format(len(listOfRequests)))
    return listOfRequests
Initializes the S3 CLient. If running in Lambda mode, only the AWS REGION environment variable is needed.     If not running in Lambda mode, the AWS credentials are also needed. Downloads the AWS ALB log file saved in S3, and input the logs into a list # Download the file from s3 # Unzip the file and read the content # Load the requests found in the file into a list # Generate an error and exit the script