Last active
September 30, 2018 16:14
-
-
Save phpfour/6533329 to your computer and use it in GitHub Desktop.
Amazon S3 with Symfony2 and Gaufrette
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 LM\Bundle\CoreBundle\Controller; | |
use Symfony\Component\HttpFoundation\Request; | |
use Symfony\Component\HttpFoundation\Response; | |
use Symfony\Bundle\FrameworkBundle\Controller\Controller; | |
class AppController extends Controller | |
{ | |
/** | |
* Upload Image to S3 | |
* | |
* @param string $name Image field name | |
* @param int $maxWidth Maximum thumb width | |
* @param int $maxHeight Maximum thumb height | |
* | |
* @return string | |
*/ | |
protected function uploadImage($name, $maxWidth = 100, $maxHeight = 100) | |
{ | |
$image = $this->getRequest()->files->get($name); | |
$uploader = $this->get('core_storage.photo_uploader'); | |
$uploadedUrl = $uploader->upload($image); | |
return $this->container->getParameter('amazon_s3_base_url') . $uploadedUrl; | |
} | |
} |
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
{ | |
"require": { | |
"knplabs/gaufrette": "dev-master", | |
"knplabs/knp-gaufrette-bundle": "dev-master", | |
"amazonwebservices/aws-sdk-for-php": "dev-master" | |
} | |
} |
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
imports: | |
- { resource: parameters.yml } | |
- { resource: security.yml } | |
- { resource: @LMCoreBundle/Resources/config/services.yml } | |
knp_gaufrette: | |
adapters: | |
photo_storage: | |
amazon_s3: | |
amazon_s3_id: loosemonkies_core.amazon_s3 | |
bucket_name: %amazon_s3_bucket_name% | |
create: false | |
options: | |
create: true | |
filesystems: | |
photo_storage: | |
adapter: photo_storage | |
alias: photo_storage_filesystem | |
loosemonkies_core: | |
amazon_s3: | |
aws_key: %amazon_aws_key% | |
aws_secret_key: %amazon_aws_secret_key% | |
base_url: %amazon_s3_base_url% |
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 LM\Bundle\CoreBundle\DependencyInjection; | |
use Symfony\Component\Config\Definition\Builder\TreeBuilder; | |
use Symfony\Component\Config\Definition\ConfigurationInterface; | |
/** | |
* This is the class that validates and merges configuration from your app/config files | |
* | |
* To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html#cookbook-bundles-extension-config-class} | |
*/ | |
class Configuration implements ConfigurationInterface | |
{ | |
/** | |
* {@inheritDoc} | |
*/ | |
public function getConfigTreeBuilder() | |
{ | |
$treeBuilder = new TreeBuilder(); | |
$rootNode = $treeBuilder->root('loosemonkies_core'); | |
// Here you should define the parameters that are allowed to | |
// configure your bundle. See the documentation linked above for | |
// more information on that topic. | |
$rootNode | |
->children() | |
->arrayNode('amazon_s3') | |
->children() | |
->scalarNode('aws_key')->end() | |
->scalarNode('aws_secret_key')->end() | |
->scalarNode('base_url')->end() | |
->end() | |
->end() | |
->end(); | |
return $treeBuilder; | |
} | |
} |
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: | |
locale: en | |
secret: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX | |
amazon_aws_key: XXXXXXXXXXXXXXXXXXXX | |
amazon_aws_secret_key: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX+ | |
amazon_s3_bucket_name: dev-assets | |
amazon_s3_base_url: 'http://s3.amazonaws.com/dev-assets/' |
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 LM\Bundle\CoreBundle\Service; | |
use Symfony\Component\HttpFoundation\File\UploadedFile; | |
use Gaufrette\Filesystem; | |
class PhotoUploader | |
{ | |
private static $allowedMimeTypes = array('image/jpeg', 'image/png', 'image/gif'); | |
private $filesystem; | |
public function __construct(Filesystem $filesystem) | |
{ | |
$this->filesystem = $filesystem; | |
} | |
public function upload(UploadedFile $file) | |
{ | |
// Check if the file's mime type is in the list of allowed mime types. | |
if (!in_array($file->getClientMimeType(), self::$allowedMimeTypes)) { | |
throw new \InvalidArgumentException(sprintf('Files of type %s are not allowed.', $file->getClientMimeType())); | |
} | |
// Generate a unique filename based on the date and add file extension of the uploaded file | |
$filename = sprintf('%s/%s/%s/%s.%s', date('Y'), date('m'), date('d'), uniqid(), $file->getClientOriginalExtension()); | |
$adapter = $this->filesystem->getAdapter(); | |
$adapter->setMetadata($filename, array('contentType' => $file->getClientMimeType())); | |
$adapter->write($filename, file_get_contents($file->getPathname())); | |
return $filename; | |
} | |
public function uploadFromUrl($url) | |
{ | |
// Get file extension | |
$extension = pathinfo($url, PATHINFO_EXTENSION); | |
// Generate a unique filename based on the date and add file extension of the uploaded file | |
$filename = sprintf('%s/%s/%s/%s.%s', date('Y'), date('m'), date('d'), uniqid(), $extension); | |
// Guess mime type | |
$mimeType = $this->guessMimeType($extension); | |
$adapter = $this->filesystem->getAdapter(); | |
$adapter->setMetadata($filename, array('contentType' => $mimeType)); | |
$adapter->write($filename, file_get_contents($url)); | |
return $filename; | |
} | |
private function guessMimeType($extension) | |
{ | |
$mimeTypes = array( | |
'txt' => 'text/plain', | |
'htm' => 'text/html', | |
'html' => 'text/html', | |
'php' => 'text/html', | |
'css' => 'text/css', | |
'js' => 'application/javascript', | |
'json' => 'application/json', | |
'xml' => 'application/xml', | |
'swf' => 'application/x-shockwave-flash', | |
'flv' => 'video/x-flv', | |
// images | |
'png' => 'image/png', | |
'jpe' => 'image/jpeg', | |
'jpeg' => 'image/jpeg', | |
'jpg' => 'image/jpeg', | |
'gif' => 'image/gif', | |
'bmp' => 'image/bmp', | |
'ico' => 'image/vnd.microsoft.icon', | |
'tiff' => 'image/tiff', | |
'tif' => 'image/tiff', | |
'svg' => 'image/svg+xml', | |
'svgz' => 'image/svg+xml', | |
// archives | |
'zip' => 'application/zip', | |
'rar' => 'application/x-rar-compressed', | |
'exe' => 'application/x-msdownload', | |
'msi' => 'application/x-msdownload', | |
'cab' => 'application/vnd.ms-cab-compressed', | |
// audio/video | |
'mp3' => 'audio/mpeg', | |
'qt' => 'video/quicktime', | |
'mov' => 'video/quicktime', | |
// adobe | |
'pdf' => 'application/pdf', | |
'psd' => 'image/vnd.adobe.photoshop', | |
'ai' => 'application/postscript', | |
'eps' => 'application/postscript', | |
'ps' => 'application/postscript', | |
// ms office | |
'doc' => 'application/msword', | |
'rtf' => 'application/rtf', | |
'xls' => 'application/vnd.ms-excel', | |
'ppt' => 'application/vnd.ms-powerpoint', | |
'docx' => 'application/msword', | |
'xlsx' => 'application/vnd.ms-excel', | |
'pptx' => 'application/vnd.ms-powerpoint', | |
// open office | |
'odt' => 'application/vnd.oasis.opendocument.text', | |
'ods' => 'application/vnd.oasis.opendocument.spreadsheet', | |
); | |
if (array_key_exists($extension, $mimeTypes)){ | |
return $mimeTypes[$extension]; | |
} else { | |
return 'application/octet-stream'; | |
} | |
} | |
} |
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
parameters: | |
core.amazon_s3.class: AmazonS3 | |
core_storage.photo_uploader.class: LM\Bundle\CoreBundle\Service\PhotoUploader | |
services: | |
loosemonkies_core.amazon_s3: | |
class: %core.amazon_s3.class% | |
arguments: | |
- { key: %amazon_aws_key%, secret: %amazon_aws_secret_key% } | |
core_storage.photo_uploader: | |
class: %core_storage.photo_uploader.class% | |
arguments: [@photo_storage_filesystem] |
With "amazonwebservices/aws-sdk-for-php": "dev-master" now deprecated [https://github.com/amazonwebservices/aws-sdk-for-php], what's the replacement for AmazonS3 class?
aws/aws-sdk-php should be he goto now
When I use aws/aws-sdk-php it shows me the error message: in composer.phar update:
[RuntimeException]
An error occurred when executing the "'cache:clear --no-warmup'" command:
Fatal error: Class 'AmazonS3' not found in /Applications/MAMP/htdocs/edutalk/app/cache/dev/appDevDebugPro
jectContainer.php on line 2420
has somebody the same problem ? Or know, what to do ?
It dosent work with the new aws-sdk-php
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for the gist..