Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 17002 | Autoría | Ultima modificación | Ver Log |

<?php
namespace LeadersLinked\Library;

use Aws\S3\S3Client;

class S3Files
{
    
    /**
     * 
     * @var\LeadersLinked\Library\S3Files
     */
    private static $_instance;

    
    /**
     * 
     * @var \Aws\S3\S3Client
     */
    private $s3;
    
    /**
     * 
     * @var string
     */
    private $bucket;
    
   
    /**
     * 
     * @param array $config
     */
    private function __construct($config)
    {
        
        $access_key     = $config['leaderslinked.minio.access_key'];
        $secret_key     = $config['leaderslinked.minio.secret_key'];
        $endpoint       = $config['leaderslinked.minio.endpoint'];
        $this->bucket   = $config['leaderslinked.minio.bucket'];
        
      
        $this->s3 = new \Aws\S3\S3Client([
            'version' => 'latest',
            'region'  => 'us-east-1',
            'endpoint' => $endpoint,
            'use_path_style_endpoint' => true,
            'credentials' => [
                'key'    => $access_key,
                'secret' => $secret_key,
            ],
        ]);
        $this->s3->registerStreamWrapper();
        
    
        
    }
    
    /**
     * 
     * @return string
     */
    public function getBucket()
    {
        return $this->bucket;
    }
    
    /**
     * 
     * @param array $config
     * @return \LeadersLinked\Library\S3Files
     */
    public static function getInstance($config)
    {

        if(self::$_instance == null) {
            self::$_instance = new S3Files($config);
        }
        
        return self::$_instance;
    }
    
    /**
     * 
  
     * @param string $key
     * @return boolean
     */
    public function objectExist($key) 
    {
        return $this->s3->doesObjectExist($this->bucket, $key);
    }
    
    /**
     * 
     * @param string $remoto
     * @return string
     */
    public function getPresignedUrl($remoto)
    {
    
        $command = $this->s3->getCommand('GetObject', [
            'Bucket' => $this->bucket,
            'Key'    => $remoto
        ]);
        
        // Create a pre-signed URL for a request with duration of 10 miniutes
        $presignedRequest = $this->s3->createPresignedRequest($command, '+10 minutes');
        
        // Get the actual presigned-url
        $presignedUrl =  (string)  $presignedRequest->getUri();
        
        return $presignedUrl;
    }

    
    /**
     * 
     * @param string $remote
     * @param string $local
     * @return boolean
     */
    public function putObject($remote, $local) 
    {

        
        try {
            $this->s3->putObject([
                'Bucket'        => $this->bucket,
                'Key'           => $remote,
                'SourceFile'    => $local
            ]);
            return true;
        } catch (\Exception $exception) {
            echo "No se pudo subir el archivo : $local (" . $exception->getMessage() . ")";

            
            return false;
        }
    }
    
   
    
    /**
     * 
     * @param string $remoto
     * @return boolean
     */
    public function deleteObject($remoto)
    {
        try {
            $this->s3->deleteObject([
                'Bucket'        => $this->bucket,
                'Key'           => $remoto,
            ]);
            return true;
        } catch (\Exception $exception) {
            //echo "No se pudo borrar el archivo : $remoto (" . $exception->getMessage() . ")";
            return false;
        }
        
    }
    

    
    
    
    
}