PHP S3 Logs Parser

News

June 19th, 2020

s3-summary.png

S3 Logs Parser is a PHP package by Alex Kolev to analyze and query AWS S3 access logs quickly. The detailed usage report includes stats for downloads and the number of bytes transferred.

This package provides a convenience class for parsing these logs with minimal effort on your part. Once you enable server access logging on an S3 bucket, here’s the gist of how you would use this package to get stats for the bucket:

use S3LogsParser\S3LogsParser;

$S3LogsParser = new S3LogsParser([
    'version' => 'latest',
    'region' => $awsBucketRegion,
    'access_key' => $awsAccessKey,
    'secret_key' => $awsSecretKey,
]);

$S3LogsParser->getStats($awsBucketName, $awsBucketPrefix, $date);

The getStats method returns a JSON-formatted string of the S3 log data:

{
    "success":true,
    "statistics":{
        "bucket":"bn-test",
        "prefix":"bp-2018-10-31",
        "data":{
            "test.png":{
                "downloads":4,
                "bandwidth":4096
            },
            "test2.png":{
                "downloads":2,
                "bandwidth":2048
            }
        }
    }
}

Learn More

You can learn more about this package, get full installation instructions, and view the source code on GitHub at mtrdesign/s3-logs-parser.

Filed in:

Paul Redmond

Full stack web developer. Author of Lumen Programming Guide and Docker for PHP Developers.