mtrdesign/s3-logs-parser

1.0.4 2022-05-22 09:29 UTC

This package is auto-updated.

Last update: 2024-04-22 00:01:33 UTC


README

Latest Stable Version Total Downloads Build Status codecov StyleCI PHPStan

AWS S3 Logs Parser is a simple PHP package to parse Amazon Simple Storage Service (Amazon S3) logs into a readable JSON format. The detailed usage report will show you how much times a file is downloaded and how much bytes are transferred.

Getting Started

  1. Sign up for AWS – Before you begin, you need to sign up for an AWS account and retrieve your AWS credentials.
  2. Create your own bucket – Now that you've signed up for Amazon S3, you're ready to create a bucket using the AWS Management Console.
  3. Enable server access logging – When you enable logging, Amazon S3 delivers access logs for a source bucket to a target bucket that you choose.
  4. Install the service – Using Composer is the recommended way to install it. The service is available via Packagist under the mtrdesign/s3-logs-parser package.
$ composer require mtrdesign/s3-logs-parser

Usage

Create a service instance:

<?php

use S3LogsParser\S3LogsParser;

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

?>

Optionally, you can set and update service configurations via setConfigs() method:

<?php

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

?>

Finally, you can get file's download and bandwidth statistics for a specific date in this way:

<?php

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

?>

It is recommended to pass Carbon date string to this method.

This is how service response should look like:

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

Contributing

Ensure all the guides are followed and style/test checkers pass before pushing your code.

Requirements

Installation steps

  1. Build the required services and Docker container with $ make docker-build
  2. SSH into the container with $ make docker-bash
  3. Confirm code style checker passes with $ make run-phpcs
  4. Confirm code quality checker passes with $ make run-phpstan
  5. Confirm code texts checker passes with $ make run-phpunit

License

AWS S3 Logs Parser is open source and available under the MIT License.