将 Symfony2 资产转储到 Amazon S3

2024-03-01

在 Symfony 2 中使用 capifony 进行部署后,我想将我的资产转储到生产中的 s3 存储桶中。我找到了一些解决方案,但并没有真正找到最好使用的解决方案。

可以使用 Zend_Service_Amazon_S3 转储资产,但我认为仅为此导入 Zend 框架有点过分了。 -http://permalink.gmane.org/gmane.comp.php.symfony.symfony2/54 http://permalink.gmane.org/gmane.comp.php.symfony.symfony2/54

我还发现了这个:https://github.com/symfony/symfony/pull/108 https://github.com/symfony/symfony/pull/108,我可以在其中告诉 AsseticBundle 存储桶名称,但我没有找到在哪里为我的 aws 帐户提供密钥和秘密。

您能否指出更好的解决方案,或者给我一些有关上述问题的详细信息。


所以我所做的一切正在发挥作用。

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>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将 Symfony2 资产转储到 Amazon S3 的相关文章

随机推荐