RapidMod

AWS s3 Example

Feb 18th, 2019 (edited)
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.54 KB | None | 0 0
  1. <?php
  2. /**
  3.  * AWS S3 Log and Error Uploader
  4.  *
  5.  * This script automatically uploads log and database error files from specific directories
  6.  * to an AWS S3 bucket, based on their last modification time. It's designed to run on EC2 instances,
  7.  * utilizing the instance's ID to organize files in the bucket. Only files modified more than 5 days ago
  8.  * are uploaded and then removed locally to prevent unnecessary storage use. This script leverages the AWS SDK for PHP.
  9.  *
  10.  * Usage:
  11.  * Execute this script as part of a cron job or manually to manage log and error file uploads.
  12.  *
  13.  * Requirements:
  14.  * AWS SDK for PHP must be included in your project, and proper IAM permissions should be set
  15.  * for the EC2 instance to interact with the specified S3 bucket.
  16.  *
  17.  * Author: RapidMod
  18.  * Website: https://rapidmod.io/
  19.  */
  20.  
  21. require_once("./awsS3.php");
  22.  
  23. $logBucket = "bucketname";
  24.  
  25. $serverInstanceID = trim(@shell_exec('ec2metadata --instance-id'));
  26.  
  27. $directories = [
  28.     "./logs" => $serverInstanceID."/PHP Logs",
  29.     "./data/db-errors" => $serverInstanceID."/DB Failures",
  30. ];
  31.  
  32. foreach($directories as $directory => $s3path) {
  33.     clearstatcache();
  34.  
  35.     $files = scandir($directory);
  36.  
  37.     if($files === FALSE) {
  38.         // @TODO: Couldn't read local directory do something ?
  39.         break;
  40.     }
  41.  
  42.     foreach($files as $file) {
  43.         if($file == ".." || $file == ".") {
  44.             continue;
  45.         }
  46.  
  47.         $sourceFile = $directory.DIRECTORY_SEPARATOR.$file;
  48.         $lastModTime = filemtime($sourceFile);
  49.  
  50.         if($lastModTime <= strtotime("-5 days")) {
  51.             try {
  52.                 $result = $awsS3->putObject([
  53.                     'Bucket' => $logBucket,
  54.                     'Key' => $s3path . DIRECTORY_SEPARATOR . $file,
  55.                     'SourceFile' => $sourceFile,
  56.                     'ContentType' => 'text/plain'
  57.                 ]);
  58.             } catch (\Aws\S3\Exception\S3Exception $e) {
  59.                 // @TODO: couldn't put object do something?
  60.                 continue;
  61.             }
  62.  
  63.             $resultsArray = $result->toArray();
  64.  
  65.             if (!isset($resultsArray['@metadata']) || !isset($resultsArray['@metadata']['statusCode']) || $resultsArray['@metadata']['statusCode'] != 200) {
  66.                 // @TODO: Couldn't upload to s3 do something?
  67.             } else {
  68.                 $unlink = @unlink($sourceFile);
  69.  
  70.                 if ($unlink === FALSE) {
  71.                     // @TODO: Couldn't remove file locally do something?
  72.                 }
  73.             }
  74.         }
  75.     }
  76. }
  77.  
  78. sleep(10);
  79.  
Advertisement
Add Comment
Please, Sign In to add comment