1

我正在尝试配置一个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:可捕获的致命错误:传递给 Knp\Bundle\GaufretteBundle\DependencyInjection\Factory\GoogleCloudStorageAdapterFactory::create() 的参数 1 必须是 Symfony\Component\DependencyInjection\ContainerBuilder 的实例,给定字符串,调用/home/amine/NetBeansProjects/tuto/app/cache/dev/appDevDebugProjectContainer.php 在第 724 行并定义

根据报错信息,我给了一个字符串,而不是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"

结果又是一个错误:

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

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

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

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');
}

我真的迷路了......那么,有没有配置谷歌云存储的完整示例?

4

1 回答 1

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

就是这样...

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

于 2015-11-23T10:32:03.137 回答