app / console assets:使用流包装器错误安装到S3

时间:2013-04-19 04:44:35

标签: php symfony amazon-s3 amazon symfony-2.1

我有一个流包装器,配置为使用Gaufrette包来管理文件系统,以便与amazon s3一起使用。我可以使用assetic成功转储资产,我的当前配置如下:

knp_gaufrette:
    adapters:
        amazon:
            amazon_s3: 
                amazon_s3_id: site_store.s3
                bucket_name: %site_store.bucket_name%
                create: true

    filesystems:
        amazon:
            adapter: amazon

    stream_wrapper:
        protocol: s3
        filesystems:
            - amazon

assetic:
    read_from:      %cdn_path_prod%
    write_to:       %cdn_path_prod%

和我的参数:

  cdn_url_prod: "http://images.site.com/"
    cdn_path_prod: "s3://amazon"

我能够做app / console资产:dump --env = dev。然后它会成功将资产上传到我的s3存储桶。但是,当我尝试通过执行以下操作来执行相同的资产安装时:

app/console assets:install s3://amazon

它给了我这个错误:

[InvalidArgumentException]  
The specified path (s3://amazon) is invalid.

我看过网络,有人能够像他描述的那样here。我的蒸汽包装有什么问题?

2 个答案:

答案 0 :(得分:2)

您确定已注册任何流包装器以处理“s3://”方案吗?

https://github.com/Cybernox/AmazonWebServicesBundle/blob/master/Resources/doc/cdn.md#dump-assets-to-the-s3-bucket中,您将看到他们如何注册流包装器以便能够将资产转储到“s3://”目标。

答案 1 :(得分:2)

所以我已经完成了它并且它正在发挥作用。

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_keyaws_regionparameters.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>