setname('utils:assets:update-static-files') ->setDescription('Update assets static files on S3 storage'); } /** * @see Command */ protected function execute(InputInterface $input , OutputInterface $output) { // AWS Doc: http://docs.aws.amazon.com/aws-sdk-php/guide/latest/service-s3.html // Confirmation part $dialog = $this->getHelperSet()->get('dialog'); if(! $dialog->askConfirmation($output, 'This command do fat stuff on your assets S3 storage! Are you sure to continue?', false)) { return; } $output->writeln('Update assets static files on S3 storage'); $yaml = new Parser(); $datas = $yaml->parse(file_get_contents($this->getContainer()->get('kernel')->getRootDir() . '/config/staticFiles.yml')); $staticFiles = $datas['staticFiles']; $bucket = $this->getContainer()->getParameter('aws_assets_bucket_name'); $s3client = $this->getContainer()->get('amazon_s3'); $s3client->registerStreamWrapper(); foreach($staticFiles as $staticFile) { $pathFile = $this->getContainer()->get('kernel')->getRootDir() . '/../web/' . $staticFile; $output->writeln('Upload ' . $staticFile . ' (' . filesize($pathFile). 'bytes)'); try { $handle = fopen($pathFile , 'r'); $stream = fopen('s3://' . $bucket . '/' . $staticFile , 'w'); fwrite($stream , fread($handle , filesize($pathFile))); fclose($stream); fclose($handle); } catch(\Exception $e){ $output->writeln('Error :-('); } } } }