Created
May 26, 2017 09:59
-
-
Save kaerer/7c50bd3a6edc9d592e80779d4c556773 to your computer and use it in GitHub Desktop.
A simple Symfony Service to Upload Files to Amazon S3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Acme\DemoBundle\Services; | |
use Aws\S3\S3Client; | |
/** | |
* Class AmazonS3Service | |
* | |
* @package Acme\DemoBundle\Service | |
*/ | |
class AmazonS3Service | |
{ | |
/** | |
* @var S3Client | |
*/ | |
private $client; | |
/** | |
* @var string | |
*/ | |
private $bucket; | |
/** | |
* @param string $bucket | |
* @param array $s3arguments | |
*/ | |
public function __construct($bucket, array $s3arguments) | |
{ | |
$this->setBucket($bucket); | |
$this->setClient(new S3Client($s3arguments)); | |
} | |
/** | |
* @param string $fileName | |
* @param string $content | |
* @param array $meta | |
* @param string $privacy | |
* @return string file url | |
*/ | |
public function upload( $fileName, $content, array $meta = [], $privacy = 'public-read') | |
{ | |
return $this->getClient()->upload($this->getBucket(), $fileName, $content, $privacy, [ | |
'Metadata' => $meta | |
])->toArray()['ObjectURL']; | |
} | |
/** | |
* @param string $fileName | |
* @param string|null $newFilename | |
* @param array $meta | |
* @param string $privacy | |
* @return string file url | |
*/ | |
public function uploadFile($fileName, $newFilename = null, array $meta = [], $privacy = 'public-read') { | |
if(!$newFilename) { | |
$newFilename = basename($fileName); | |
} | |
if(!isset($meta['contentType'])) { | |
// Detect Mime Type | |
$mimeTypeHandler = finfo_open(FILEINFO_MIME_TYPE); | |
$meta['contentType'] = finfo_file($mimeTypeHandler, $fileName); | |
finfo_close($mimeTypeHandler); | |
} | |
return $this->upload($newFilename, file_get_contents($fileName), $meta, $privacy); | |
} | |
/** | |
* Getter of client | |
* | |
* @return S3Client | |
*/ | |
protected function getClient() | |
{ | |
return $this->client; | |
} | |
/** | |
* Setter of client | |
* | |
* @param S3Client $client | |
* | |
* @return $this | |
*/ | |
private function setClient(S3Client $client) | |
{ | |
$this->client = $client; | |
return $this; | |
} | |
/** | |
* Getter of bucket | |
* | |
* @return string | |
*/ | |
protected function getBucket() | |
{ | |
return $this->bucket; | |
} | |
/** | |
* Setter of bucket | |
* | |
* @param string $bucket | |
* | |
* @return $this | |
*/ | |
private function setBucket($bucket) | |
{ | |
$this->bucket = $bucket; | |
return $this; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
php composer.phar require "aws/aws-sdk-php" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This file is auto-generated during the composer install | |
parameters: | |
.... | |
amazon_key: | |
amazon_secret: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
services: | |
.... | |
demo_storage: | |
class: Acme\DemoBundle\Service\AmazonS3Service | |
arguments: | |
- "demo" # Name of Bucket | |
- credentials: | |
key: "%amazon_key%" | |
secret: "%amazon_secret%" | |
region: "eu-central-1" # Region of Bucket | |
version: "2006-03-01" # API Version |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$storage = $this->getContainer()->get('demo_storage'); | |
// Upload a file with the content "content text" and the MIME-Type text/plain | |
$storage->upload('test.txt', 'content text', ['contentType' => 'text/plain']); | |
// Upload a local existing File and let the service automatically detect the mime type. | |
$storage->uploadFile('file path' . 'demo.pdf'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment