src/Services/AmazonS3Service.php line 25

Open in your IDE?
  1. <?php
  2. namespace App\Services;
  3. use Aws\S3\S3Client;
  4. /**
  5.  * Class AmazonS3Service
  6.  *
  7.  * @package Acme\DemoBundle\Service
  8.  */
  9. class AmazonS3Service
  10. {
  11.     /**
  12.      * @var S3Client
  13.      */
  14.     private $client;
  15.     /**
  16.      * @var string
  17.      */
  18.     private $bucket;
  19.     /**
  20.      * @param string $bucket
  21.      * @param array  $s3arguments
  22.      */
  23.     public function __construct($bucket, array $s3arguments)
  24.     {
  25.         $this->setBucket($bucket);
  26.         $this->setClient(new S3Client($s3arguments));
  27.     }
  28.     /**
  29.      * @param string $fileName
  30.      * @param string $content
  31.      * @param array  $meta
  32.      * @param string $privacy
  33.      * @return string file url
  34.      */
  35.     public function upload$fileName$content, array $meta = [], $privacy 'public-read')
  36.     {
  37.         return $this->getClient()->upload($this->getBucket(), $fileName$content$privacy, [
  38.             'Metadata' => $meta
  39.         ])->toArray()['ObjectURL'];
  40.     }
  41.     
  42.  
  43.     public function uploadFileToBucket($key,$file){
  44.             // Upload data.
  45.             $result $this->getClient()->putObject([
  46.                     'Bucket' => $this->getBucket(),
  47.                     'Key'    => $key,
  48.                     //'Body'   => 'Hello, world!',
  49.                     'ACL'    => 'public-read',
  50.                     'SourceFile' => $file
  51.                     
  52.           ]);
  53.      
  54.         return $result->toArray()['ObjectURL'];
  55.     }
  56.     
  57.     
  58.     
  59.     
  60.     /**
  61.      * @param string       $fileName
  62.      * @param string|null  $newFilename
  63.      * @param array        $meta
  64.      * @param string       $privacy
  65.      * @return string file url
  66.      */
  67.     public function uploadFile($fileName$newFilename null, array $meta = [], $privacy 'public-read') {
  68.         if(!$newFilename) {
  69.             $newFilename basename($fileName);
  70.         }
  71.         if(!isset($meta['contentType'])) {
  72.             // Detect Mime Type
  73.             $mimeTypeHandler finfo_open(FILEINFO_MIME_TYPE);
  74.             $meta['contentType'] = finfo_file($mimeTypeHandler$fileName);
  75.             finfo_close($mimeTypeHandler);
  76.         }
  77.         return $this->upload($newFilenamefile_get_contents($fileName), $meta$privacy);
  78.     }
  79.     /**
  80.      * Getter of client
  81.      *
  82.      * @return S3Client
  83.      */
  84.     protected function getClient()
  85.     {
  86.         return $this->client;
  87.     }
  88.     /**
  89.      * Setter of client
  90.      *
  91.      * @param S3Client $client
  92.      *
  93.      * @return $this
  94.      */
  95.     private function setClient(S3Client $client)
  96.     {
  97.         $this->client $client;
  98.         return $this;
  99.     }
  100.     /**
  101.      * Getter of bucket
  102.      *
  103.      * @return string
  104.      */
  105.     protected function getBucket()
  106.     {
  107.         return $this->bucket;
  108.     }
  109.     /**
  110.      * Setter of bucket
  111.      *
  112.      * @param string $bucket
  113.      *
  114.      * @return $this
  115.      */
  116.     private function setBucket($bucket)
  117.     {
  118.         $this->bucket $bucket;
  119.         return $this;
  120.     }
  121. }