Python: Uploading S3 statistics into DynamoDB

Honestly, this is something I will probably never use because I found out that even when uploading data to DynamoDB using Boto is fairly easy, it is much more difficult to analyze data in NoSQL database.

You can use this file e.g. if you want to parse ‘logs’ directory containing S3 access logs running logs

#!/usr/bin/env python3
import boto3, re, json, hashlib, sys, os

dynamodb = boto3.resource('dynamodb', region_name='eu-west-1', endpoint_url="")

table = dynamodb.Table('s3_logs')


def parse_file(file):
        regex = '(\w+) ([\w_-]+) \[(.*?)\] ([(\d\.)]+) - (\w+) ([\w\.]*) (.+?) "(.*?)" (\d+) - (\d+) (\d+) (\d+) (\d+) "(.*?)" "(.*?)" -'

        with open(file) as f:
            line = f.readline()

        match = re.match(regex, line).groups()

        response = table.put_item(
                'id': hashlib.sha224(line.encode('utf-8')).hexdigest(),
                'repository': match[1],
                'date': match[2],
                'ip': match[3],
                'item': match[6],
                'referer': match[13],
                'agent': match[14]

        print("PutItem succeeded")
        print("PutItem failed")

inputdir = sys.argv[1]

for file in os.listdir(inputdir):
    parse_file(inputdir + '/' + file)