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



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

## definition of the GCS service
app.google_cloud_storage.service:
    class: Google_Service_Storage
    factory_class: KnpBundleGaufretteBundleDependencyInjectionFactoryGoogleCloudStorageAdapterFactory
    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中的ContextErrorException第16行:可捕获的致命错误:传递给Knp\Bundle\GaufretteBundle\DependencyInjection\Factory\GoogleCloudStorageAdapterFactory::create()的参数1必须是Symfony\Component\Dependency Injection\ContainerBuilder的实例,给定字符串,在/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: KnpBundleGaufretteBundleDependencyInjectionFactoryGoogleCloudStorageAdapterFactory
    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\Dependency Injection\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'] =KnpBundleGaufretteBundleDependencyInjectionFactoryGoogleCloudStorageAdapterFactory::create($this, '123@developer.gserviceaccount.com', 'http://localhost/file.p12', 'pwd');
}

我真的很失落。。。那么,有没有完整的例子来配置谷歌云存储?

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

工厂等级

<?php
namespace MinnAdsBundleFactory;
/**
 * 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: [MinnAdsBundleFactoryGoogleCloudStorageServiceFactory, 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

就是这样…

希望它能帮助其他人。。。

相关内容

  • 没有找到相关文章

最新更新