Skip to content
Snippets Groups Projects
Select Git revision
2 results Searching

S3_files.class.inc

Blame
  • Forked from Open source / vMap
    Source project has a limited visibility.
    S3_files.class.inc 10.66 KiB
    <?php
    
    require_once ("Files.interface.inc");
    require_once ("vmlib/phpUtil.inc");
    require_once ("vmlib/logUtil.inc");
    require_once ("aws_sdk/aws-autoloader.php");
    
    class S3_files implements Files{
    
        var $oProperties;
        var $oS3Client;
    
        function __construct($properties){
            $this->oProperties = $properties;
            // create client s3
            $this->oS3Client = new Aws\S3\S3Client(array(
                'version'=>'latest',
                'region'=> $properties['fileS3UploaderRegion'],
                'profile'=> $properties['fileS3UploaderProfil'],
                'debug' => false
            ));
        }
    
        private function getBucketConst(){
            $sBucket = $this->oProperties['fileS3UploaderBucket'];
            $sPrefix = "";
    
            if (strpos($sBucket, "/") > -1){
                $aBucket = explode("/", $sBucket );
                $sBucket = $aBucket[0];
                $sPrefix = implode("/", array_slice($aBucket, 1));
            }
    
            return array($sBucket, $sPrefix);
        }
        public function getFileEtag($sFilePath){
            list($sBucket, $sPrefix) = $this->getBucketConst();
            $sFilePath = str_replace($this->oProperties["vas_home"], $sPrefix , $sFilePath);
    
            try {
                $oResult = $this->oS3Client->headObject(array(
                    'Bucket' => $sBucket,
                    'Key'    => $sFilePath)
                );
    
                return str_replace('"', '', $oResult["ETag"]);
            }catch (Aws\S3\Exception\S3Exception $e){
                writeToErrorLog($e->getMessage());
                return false;
            }
        }
        public function file_exists ($sFilePath){
    
            list($sBucket, $sPrefix) = $this->getBucketConst();
            $sFilePath = str_replace($this->oProperties["vas_home"], $sPrefix , $sFilePath);
    
            try {
                $this->oS3Client->headObject(array(
                    'Bucket' => $sBucket,
                    'Key'    => $sFilePath)
                );
            }catch (Aws\S3\Exception\S3Exception $e){
                writeToErrorLog($e->getMessage());
                return false;
            }
            return true;
        }
        public function file_put_contents ($sFilePath, $sData, $iFlags = 0, $mContext = null){
            list($sBucket, $sPrefix) = $this->getBucketConst();
            $sFilePath = str_replace($this->oProperties["vas_home"], $sPrefix , $sFilePath);