Dump Symfony2 assets to Amazon S3

后端 未结 3 531
栀梦
栀梦 2021-01-30 15:08

I\'d like to dump my assets to my s3 bucket in production, after deploying with capifony in Symfony 2. I\'ve found some solution, but don\'t really find out the best to use.

3条回答
  •  独厮守ぢ
    2021-01-30 15:46

    So what I've done and it is working.

    Add at composer.json and install it

    "aws/aws-sdk-php": "2.6.16",
    

    Create a service:

     $key,
                'secret' => $secret,
                'region' => $region
            );
    
            $this->s3 = Aws::factory($aws)->get('s3');
    
        }
    
        public function registerStreamWrapper() {
            $this->s3->registerStreamWrapper();
        }
    
    }
    

    Declare the service atconfig.yml or including it as a file

    services:
        my_amazon_s3:
            class: My\AcmeBundle\Amazon\StreamWrapperS3
            arguments: [%aws_key%, %aws_secret_key%, %aws_region%]
    

    Add the parameters at parameters.yml

    Override boot() method at AppKernel.php:

    public function boot() {
        parent::boot();
        $s3client = $this->container->get('my_amazon_s3');;
        $s3client->registerStreamWrapper();
    }
    

    At config_prod.yml add:

    framework:
        templating:
            assets_base_url: https://sa-east-1.amazonaws.com/your-bucket-name
    assetic:
        write_to: 's3://your-bucket-name'
    

    Finally add the filter with your assets to rewrite correctly your paths:

    {% stylesheets filter='cssrewrite'
        'bundles/...' %}
         {# asset just to be sure that url will be right #}
    {% endstylesheets %}
    

    So each time that you've changed something need to run:

    php app/console cache:clear --env=prod
    php app/console assets:install --env=prod
    php app/console assetic:dump --env=prod
    

    A very important detail that took almost 2 days of my time, you need to update CORS of Amazon S3 to access some files as fonts add inside twitter bootstrap css for example. My CORS permissions are like this:

    
    
        
            *
            GET
            PUT
            POST
            DELETE
            3000
            *
        
    
    

提交回复
热议问题