关于如何使用 KnpGaufetteBundle 配置 google 云存储的示例

Example on how to config google cloud storage with KnpGaufetteBundle

我正在尝试配置 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 的实例,给定字符串,在第 724 行的 /home/amine/NetBeansProjects/tuto/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"

结果又报错:

可捕获的致命错误:传递给 Knp\Bundle\GaufretteBundle\DependencyInjection\Factory\GoogleCloudStorageAdapterFactory::create() 的参数 1 必须是 Symfony\Component\DependencyInjection\ContainerBuilder 的实例,给定的 appDevDebugProjectContainer 实例,在 [=41= 中调用].php 在第 724 行并定义了

所以现在,错误告诉我我提供了一个 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云存储?

我终于找到了解决办法。您必须创建自己的工厂 class,如捆绑文档中所述:

工厂class

<?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

就是这样...

希望对其他人有所帮助...