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.
- Sign up for AWS – Before you begin, you need to sign up for an AWS account and retrieve your AWS credentials.
- 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.
- Enable server access logging – When you enable logging, Amazon S3 delivers access logs for a source bucket to a target bucket that you choose.
- 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
Extracts statistics much more quickly if you have already downloaded the logs to local storage with something like AWS CLI aws s3 sync
.
<?php
$S3LogsParser->setConfigs([
'version' => 'latest',
'local_log_dir' => 'path/to/logs/that_i_already_downloaded/'
]);
?>
Via direct instantiation of S3LogsParser
object:
<?php
use S3LogsParser\S3LogsParser;
$S3LogsParser = new S3LogsParser([
'version' => 'latest',
'region' => $awsBucketRegion,
'access_key' => $awsAccessKey,
'secret_key' => $awsSecretKey,
]);
?>
By Setting configuration parameters:
<?php
$S3LogsParser->setConfigs([
'version' => 'latest',
'region' => $awsBucketRegion,
'access_key' => $awsAccessKey,
'secret_key' => $awsSecretKey,
]);
?>
Things like download
, bandwidth
, etc.
$date
is an optional param. Pass a Carbon formatted date string. S3 logs tend to have filenames that look like 2022-05-02-19-18-32-91D293838329CB5E6
. If $date
is provided the %Y-%m-%d
formatted date string will be used as a prefix to match log filenames.
$S3LogsParser->getStats($awsBucketName, $awsBucketPrefix, $date);
getStats()
response should be something like this:
{
"success":true,
"statistics":{
"bucket":"bn-test",
"prefix":"bp-2018-10-31",
"data":{
"test.png":{
"downloads":4,
"bandwidth":4096
},
"test2.png":{
"downloads":2,
"bandwidth":2048
}
}
}
}
Ensure all the guides are followed and style/test checkers pass before pushing your code.
- Build the required services and Docker container with
$ make docker-build
- SSH into the container with
$ make docker-bash
- Confirm code style checker passes with
$ make run-phpcs
- Confirm code quality checker passes with
$ make run-phpstan
- Confirm code texts checker passes with
$ make run-phpunit
You can set the optional configuration parameter debug_mode
to see a more verbose output.
$S3LogsParser = new S3LogsParser\S3LogsParser([
'local_log_dir' => '/path/to/logs',
'debug_mode' => 'true',
]);
AWS S3 Logs Parser is open source and available under the MIT License.