Skip to content

Instantly share code, notes, and snippets.

@S4lem
Created August 7, 2022 22:10
Show Gist options
  • Save S4lem/5575cfd56568925f23cf9cdbd037ff5a to your computer and use it in GitHub Desktop.
Save S4lem/5575cfd56568925f23cf9cdbd037ff5a to your computer and use it in GitHub Desktop.
PITOS3
<?php
//Include AWS SDK
require '/home/bitnami/vendor/autoload.php';
//Start S3 client
$s3 = new Aws\S3\S3Client([
'region' => 'us-west-2',
'version' => 'latest',
'credentials' => [
'key' => '<iamkey>', //IAM user key
'secret' => '<iamsecret>', //IAM user secret
]
]);
//Set timezone and get current time
date_default_timezone_set('America/Los_Angeles');
$currentTime=strtotime("now");
//Get a list of all the items in the directory, ignoring those we don't want to mess with
$files = array_diff(scandir("/home/bitnami/recordings",1), array('.', '..','.mp4','_cron_camsstorevideos.sh'));
//Loop through those files
foreach($files as $file){
$lastModified=date ("Y-m-d H:i:s", filemtime("/home/bitnami/recordings/$file"));//Separate out the "pretty" timestamp as we'll use it to rename our files.
$lastModifiedEpoch=strtotime($lastModified);//Get the last modified time
if($currentTime-$lastModifiedEpoch>30){ //If the difference between now and when the file was last modified is > 30 seconds (meaning it's finished writing to disk), take actions
echo "\r\n Taking action! $file was last modified: " . date ("F d Y H:i:s", filemtime("/home/bitnami/recordings/$file"));
//Save to S3
$result = $s3->putObject([
'Bucket' => '<bucketname>', //the S3 bucket name you're using
'Key' => "CAM1VIDEO @ $lastModified.mp4", //The new filename/S3 key for our video (we'll use the last modified time for this)
'SourceFile' => "/home/bitnami/recordings/$file", //The source file for our video
'StorageClass' => 'ONEZONE_IA' //I'm using one zone, infrequent access (IA) storage for this because it's cheaper
]);
//Delete file from lightsail
unlink("/home/bitnami/recordings/$file");
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment