RapidMod icon

AWS s3 Example

RapidMod | PRO | 04/04/24 01:36:25 PM UTC (Edited) | 0 ⭐ | 284 👁️ | Never ⏰ | []
PHP |

2.54 KB

|

None

|

0 👍

/

0 👎

<?php
/**
 * AWS S3 Log and Error Uploader
 *
 * This script automatically uploads log and database error files from specific directories
 * to an AWS S3 bucket, based on their last modification time. It's designed to run on EC2 instances,
 * utilizing the instance's ID to organize files in the bucket. Only files modified more than 5 days ago
 * are uploaded and then removed locally to prevent unnecessary storage use. This script leverages the AWS SDK for PHP.
 *
 * Usage:
 * Execute this script as part of a cron job or manually to manage log and error file uploads.
 *
 * Requirements:
 * AWS SDK for PHP must be included in your project, and proper IAM permissions should be set
 * for the EC2 instance to interact with the specified S3 bucket.
 *
 * Author: RapidMod
 * Website: https://rapidmod.io/
 */
 
require_once("./awsS3.php");
 
$logBucket = "bucketname";
 
$serverInstanceID = trim(@shell_exec('ec2metadata --instance-id'));
 
$directories = [
    "./logs" => $serverInstanceID."/PHP Logs",
    "./data/db-errors" => $serverInstanceID."/DB Failures",
];
 
foreach($directories as $directory => $s3path) {
    clearstatcache();
 
    $files = scandir($directory);
 
    if($files === FALSE) {
        // @TODO: Couldn't read local directory do something ?
        break;
    }
 
    foreach($files as $file) {
        if($file == ".." || $file == ".") {
            continue;
        }
 
        $sourceFile = $directory.DIRECTORY_SEPARATOR.$file;
        $lastModTime = filemtime($sourceFile);
 
        if($lastModTime <= strtotime("-5 days")) {
            try {
                $result = $awsS3->putObject([
                    'Bucket' => $logBucket,
                    'Key' => $s3path . DIRECTORY_SEPARATOR . $file,
                    'SourceFile' => $sourceFile,
                    'ContentType' => 'text/plain'
                ]);
            } catch (\Aws\S3\Exception\S3Exception $e) {
                // @TODO: couldn't put object do something?
                continue;
            }
 
            $resultsArray = $result->toArray();
 
            if (!isset($resultsArray['@metadata']) || !isset($resultsArray['@metadata']['statusCode']) || $resultsArray['@metadata']['statusCode'] != 200) {
                // @TODO: Couldn't upload to s3 do something?
            } else {
                $unlink = @unlink($sourceFile);
 
                if ($unlink === FALSE) {
                    // @TODO: Couldn't remove file locally do something?
                }
            }
        }
    }
}
 
sleep(10);
 

Comments