Vì vậy, những gì tôi đã làm và nó đang hoạt động.
Thêm ít composer.json
và cài đặt nó
"aws/aws-sdk-php": "2.6.16",
Tạo một dịch vụ:
<?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();
}
}
Khai báo các dịch vụ tại config.yml
hay kể cả nó như là một tập tin
services:
my_amazon_s3:
class: My\AcmeBundle\Amazon\StreamWrapperS3
arguments: [%aws_key%, %aws_secret_key%, %aws_region%]
Thêm các thông số tại parameters.yml
Override boot()
phương pháp tại AppKernel.php
:
public function boot() {
parent::boot();
$s3client = $this->container->get('my_amazon_s3');;
$s3client->registerStreamWrapper();
}
Tại 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'
Cuối cùng thêm bộ lọc với tài sản của mình để viết lại một cách chính xác đường dẫn của bạn:
{% stylesheets filter='cssrewrite'
'bundles/...' %}
<link rel="stylesheet" href="{{ asset_url }}" /> {# asset just to be sure that url will be right #}
{% endstylesheets %}
Vì vậy, mỗi thời gian mà bạn đã thay đổi một cái gì đó cần phải chạy:
php app/console cache:clear --env=prod
php app/console assets:install --env=prod
php app/console assetic:dump --env=prod
Một chi tiết rất quan trọng mất gần 2 ngày thời gian của tôi, bạn cần cập nhật CORS của Amazon S3 để truy cập một số tệp như phông chữ thêm bên trong css bootstrap css chẳng hạn. Quyền của CORS của tôi như sau:
<?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>
Tôi nên thêm gì khác trong config_prod? Khi tôi chạy: 'php app/console assetic: dump s3: // my-cucket --env = prod' nó trả về' PHP Warning: is_dir(): Không thể tìm thấy trình bao bọc "s3" - bạn đã quên bật nó chưa khi bạn cấu hình PHP? ' – Cassiano
@Cassiano Tôi gặp phải vấn đề tương tự. Bạn đã giải quyết nó chưa? – Reusable
@Không thể sử dụng giải pháp của tôi ở trên http://stackoverflow.com/a/26146698/1983572 – Cassiano