how to upload files to Google Cloud Storage with s

2020-06-26 20:06发布

问题:

Good morning,

I am uploading my files locally through VichUploaderBundle. Every thing works perfectly.

Now I want no more store my files locally: I want to store them on Google Cloud Storage. I found that KnpGaufretteBundle could be used for storing files in the cloud.

So, is there any example or a example's link on how to configure Gaufrette (the php package) or KnpGaufretteBundle (the symfony bundle) for storing/uploading/reading files on google-cloud-storage?

The only thing I found is that link which is an adapter class for GoogleCloudStorage.

I found another link regarding this issue but it proposes an example for storing files on amazon-s3: upload-files-to-amazon-s3-with-symfony2-and-gaufrette

Thanks for you help...

回答1:

Is possible to use KnpGaufretteBundle to do that. You only need to create a factory that creates a Google Client fully authenticated and return the Google Service Storage with that Client. Something like that:

/**
 * @return \Google_Service_Storage
 */
public function createService($privateKeyPath, $password, $googleAccountName)
{
    $client = new \Google_Client();
    $scopes = array(
        'https://www.googleapis.com/auth/devstorage.read_write'
    );

    $privateKey = file_get_contents($privateKeyPath);

    $credential = new \Google_Auth_AssertionCredentials(
        $googleAccountName, $scopes, $privateKey, $password
    );

    $client->setAssertionCredentials($credential);

    return new \Google_Service_Storage($client);
}

You also need to configure the KnpGaufretteBundle, for this look at: KnpGaufretteBundle-GoogleCloudStorage

Here there's a post in GitHub where explains the factory issue: post