簡體   English   中英

有關如何使用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"

結果又是一個錯誤:

可捕獲的致命錯誤:參數1傳遞給Knp \\ Bundle \\ GaufretteBundle \\ DependencyInjection \\ Factory \\ GoogleCloudStorageAdapterFactory :: create()必須是Symfony \\ Component \\ DependencyInjection \\ ContainerBuilder的實例,appDevDebugProjectContainer的實例,在/ home / amine / NetBeansProjects中調用/tuto/app/cache/dev/appDevDebugProjectContainer.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');
}

我真的迷路了...那么,有沒有完整的例子來配置谷歌雲存儲?

我終於找到了解決方案。 您必須按照軟件包文檔中的描述創建自己的工廠類:

工廠類

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

就是這樣......

希望它會幫助別人......

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM