有关如何使用KnpGaufetteBundle

时间:2015-11-14 06:40:52

标签: symfony google-cloud-storage gaufrette

我正在尝试配置KnpGaufretteBundle以使用Google Cloud Storage来存储我的文件。这是配置:

## definition of the GCS service
app.google_cloud_storage.service:
    class: \Google_Service_Storage
    factory_class: Knp\Bundle\GaufretteBundle\DependencyInjection\Factory\GoogleCloudStorageAdapterFactory
    factory_method: 'create'
    arguments: 
        - "123@developer.gserviceaccount.com"
        - "http://localhost/file.p12"
        - "pwd"

## config of knp_gaufrette
knp_gaufrette:
    stream_wrapper: ~
    adapters:
        gcs_minn_images:
            google_cloud_storage:
                service_id: 'app.google_cloud_storage.service'
                bucket_name: 'minn-images'
    filesystems:
        gcs_minn_images_fs:
            adapter: gcs_minn_images 

我收到的错误信息是:

GoogleCloudStorageAdapterFactory.php第16行中的ContextErrorException: 可捕获致命错误:参数1传递给Knp \ Bundle \ GaufretteBundle \ DependencyInjection \ Factory \ GoogleCloudStorageAdapterFactory :: create()必须是Symfony \ Component \ DependencyInjection \ ContainerBuilder的实例,给定字符串,在/ home / amine / NetBeansProjects / tuto中调用第724行的/app/cache/dev/appDevDebugProjectContainer.php并定义了

根据错误消息,我给了一串代替ContainerBuilder的字符串。大!让我们将ContainerBuilder添加到参数中,如下所示:

## definition of the GCS service
app.google_cloud_storage.service:
    class: \Google_Service_Storage
    factory_class: Knp\Bundle\GaufretteBundle\DependencyInjection\Factory\GoogleCloudStorageAdapterFactory
    factory_method: 'create'
    arguments: 
        - @service_container
        - "123@developer.gserviceaccount.com"
        - "http://localhost/file.p12"
        - "pwd"

结果又是一个错误:

捕获致命错误:参数1传递给Knp \ Bundle \ GaufretteBundle \ DependencyInjection \ Factory \ GoogleCloudStorageAdapterFactory :: create()必须是Symfony \ Component \ DependencyInjection \ ContainerBuilder的实例,appDevDebugProjectContainer的实例给出,调用第724行/home/amine/NetBeansProjects/tuto/app/cache/dev/appDevDebugProjectContainer.php并定义

现在,错误告诉我,我提供了appDevDebugProjectContainer的实例,而不是ContainerBuilder !!

好的,让我们看看第724行的/home/amine/NetBeansProjects/tuto/app/cache/dev/appDevDebugProjectContainer.php ......

class appDevDebugProjectContainer extends Container{
// ...
/**
 * Gets the 'app.google_cloud_storage.service' service.
 *
 * This service is shared.
 * This method always returns the same instance of the service.
 *
 * @return \Google_Service_Storage A Google_Service_Storage instance.
 */
protected function getApp_GoogleCloudStorage_ServiceService()
{
    return $this->services['app.google_cloud_storage.service'] =\Knp\Bundle\GaufretteBundle\DependencyInjection\Factory\GoogleCloudStorageAdapterFactory::create($this, '123@developer.gserviceaccount.com', 'http://localhost/file.p12', 'pwd');
}

我真的迷失了...... 那么,是否有任何完整示例来配置Google云端存储?

1 个答案:

答案 0 :(得分:3)

我终于找到了解决方案。您必须按照软件包文档中的描述创建自己的工厂类:

工厂类

<?php

namespace Minn\AdsBundle\Factory;
/**
 * Description of GoogleCloudStorageServiceFactory
 */
class GoogleCloudStorageServiceFactory {

    public function createService() {
        // creating the google client
        $client = new \Google_Client();

        // setting the service acount credentials
        $serviceAccountName = '123@developer.gserviceaccount.com';
        $scopes = array(
            'https://www.googleapis.com/auth/devstorage.read_write',
        );
        $privateKey = file_get_contents('http://localhost/f.p12');
        $privateKeyPassword = 'pwd';

        $credential = new \Google_Auth_AssertionCredentials(
                $serviceAccountName, $scopes, $privateKey, $privateKeyPassword);

        // set assertion credentials
        $client->setAssertionCredentials($credential);

        // creating and returning the service
        return new \Google_Service_Storage($client);
    }

}

config.yml文件

app.google_cloud_storage.service:
    class: \Google_Service_Storage
    factory: [Minn\AdsBundle\Factory\GoogleCloudStorageServiceFactory, createService]


knp_gaufrette:
    stream_wrapper: ~
    adapters:
        gcs_images:
            google_cloud_storage:
                service_id: 'app.google_cloud_storage.service'
                bucket_name: 'images'
filesystems:
    gcs_images_fs:
        adapter: gcs_images

vich_uploader:
    db_driver: orm
    storage: gaufrette   
    mappings:
        motors_files:
            upload_destination: gcs_images_fs
            namer: vich_uploader.namer_origname
            delete_on_remove: true

就是这样......

希望它会帮助别人......