所以我做了什么,它正在工作。
添加composer.json
并安装它
"aws/aws-sdk-php": "2.6.16",
创建类:
<?php
namespace My\AcmeBundle\Amazon;
use Aws\Common\Aws;
class StreamWrapperS3 {
protected $s3;
public function __construct($key, $secret, $region) {
$aws = array(
'key' => $key,
'secret' => $secret,
'region' => $region
);
$this->s3 = Aws::factory($aws)->get('s3');
}
public function registerStreamWrapper() {
$this->s3->registerStreamWrapper();
}
}
添加参数:aws_key
, aws_secret_key
, aws_region
inparameters.yml
覆盖boot()
方法在AppKernel.php
:
public function boot() {
parent::boot();
$s3client = new \Path\to\Amazon\StreamWrapperS3($this->container->getParameter('aws_key'), $this->container->getParameter('aws_secret_key'), $this->container->getParameter('aws_region'));
$s3client->registerStreamWrapper();
}
在config_prod.yml
添加:
framework:
templating:
assets_base_url: https://sa-east-1.amazonaws.com/your-bucket-name
assetic:
write_to: 's3://your-bucket-name'
最后将过滤器与您的资产一起添加以正确重写您的路径:
{% stylesheets filter='cssrewrite'
'bundles/...' %}
<link rel="stylesheet" href="{{ asset(asset_url) }}" /> {# asset just to be sure that url will be right #}
{% endstylesheets %}
因此,每次您更改某些内容时都需要运行:
php app/console cache:clear --env=prod
php app/console assets:install s3://<your-bucket-name> --env=prod
php app/console assetic:dump --env=prod
一个非常重要的细节花了我将近 2 天的时间,您需要更新 Amazon S3 的 CORS 以访问一些文件,例如在 twitter bootstrap css 中添加的字体。我的 CORS 权限是这样的:
<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<CORSRule>
<AllowedOrigin>*</AllowedOrigin>
<AllowedMethod>GET</AllowedMethod>
<AllowedMethod>PUT</AllowedMethod>
<AllowedMethod>POST</AllowedMethod>
<AllowedMethod>DELETE</AllowedMethod>
<MaxAgeSeconds>3000</MaxAgeSeconds>
<AllowedHeader>*</AllowedHeader>
</CORSRule>
</CORSConfiguration>