所以我所做的一切正在发挥作用。
Add at 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();
}
}
声明服务于config.yml
或将其作为文件包含
services:
my_amazon_s3:
class: My\AcmeBundle\Amazon\StreamWrapperS3
arguments: [%aws_key%, %aws_secret_key%, %aws_region%]
添加参数于parameters.yml
覆盖boot()
方法在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'
最后添加带有资产的过滤器以正确重写您的路径:
{% stylesheets filter='cssrewrite'
'bundles/...' %}
<link rel="stylesheet" href="{{ 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 --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>