You could find more information about this gist in the following URL: https://merianos.wordpress.com/2021/09/11/symfony-5-with-flysystem-and-google-cloud-storage/
Last active
December 3, 2022 06:56
-
-
Save merianos/fced2f619fe9e667f5cefdf861f70f14 to your computer and use it in GitHub Desktop.
Symfony 5 with FlySystem and Google Cloud Storage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Values for the following keys can be loaded from the JSON Key generated from Google | |
// Cloud Console under the Service Accounts section. | |
$keyFile = [ | |
'type' => '', | |
'project_id' => '', | |
'private_key_id' => '', | |
'private_key' => '', | |
'client_email' => '', | |
'client_id' => '', | |
'auth_uri' => '', | |
'token_uri' => '', | |
'auth_provider_x509_cert_url' => '', | |
'client_x509_cert_url' => '', | |
]; | |
$client = new StorageClient( | |
[ | |
'projectId' => $keyFile['project_id'], | |
'keyFile' => $keyFile, | |
] | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# config/packages/flysystem.yaml | |
flysystem: | |
storages: | |
default.storage: | |
adapter: 'local' | |
options: | |
directory: '%kernel.project_dir%/var/storage/default' | |
# This is my custom created storage for the users profile pictures. | |
profile_pictures.storage: | |
adapter: 'gcloud' | |
options: | |
# Add the path to your GoogleCloudStorage Bucket Service | |
client: 'App\Services\Storage\GoogleCloudStorage' | |
# Enter your bucket name | |
bucket: 'bucket-name' | |
# The following property it is optional. | |
# Use it only if you want to have one bucket, and organize the files for that storage in a folder. | |
prefix: 'some-prefix' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use App\Entity\VichUploadableEntity; | |
use Vich\UploaderBundle\Storage\StorageInterface; | |
class GetPictureFileFullUrl { | |
private StorageInterface $storage; | |
public function __construct(StorageInterface $storage) { | |
$this->storage = $storage; | |
} | |
public function getURL( VichUploadableEntity $picture ) { | |
$pictureUrl = $this->storage->resolveUri($picture, 'file'); | |
return $pictureUrl; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Services\Storage; | |
use Google\Cloud\Storage\Bucket; | |
use Google\Cloud\Storage\StorageClient; | |
/** | |
* This class is responsible to return a new Google Cloud Storage bucket instance. | |
*/ | |
class GoogleCloudStorage { | |
/** | |
* This method is auto executed from the FlySystem when initializing the Google Cloud Storage. | |
* | |
* @param string $bucket The bucket name. | |
* | |
* @return Bucket The bucket instance. | |
*/ | |
public function bucket( string $bucket = '' ) : Bucket { | |
$keyFile = [ | |
'type' => $_SERVER['GCS_TYPE'] ?? '', | |
'project_id' => $_SERVER['GCS_PROJECT_ID'] ?? '', | |
'private_key_id' => $_SERVER['GCS_PRIVATE_KEY_ID'] ?? '', | |
'private_key' => $_SERVER['GCS_PRIVATE_KEY'] ?? '', | |
'client_email' => $_SERVER['GCS_CLIENT_EMAIL'] ?? '', | |
'client_id' => $_SERVER['GCS_CLIENT_ID'] ?? '', | |
'auth_uri' => $_SERVER['GCS_AUTH_URI'] ?? '', | |
'token_uri' => $_SERVER['GCS_TOKEN_URI'] ?? '', | |
'auth_provider_x509_cert_url' => $_SERVER['GCS_AUTH_PROVIDER_X509_CERT_URL'] ?? '', | |
'client_x509_cert_url' => $_SERVER['GCS_CLIENT_X509_CERT_URL'] ?? '', | |
]; | |
$client = new StorageClient( | |
[ | |
'projectId' => $keyFile['project_id'], | |
'keyFile' => $keyFile, | |
] | |
); | |
return $client->bucket( $bucket ); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
vich_uploader: | |
db_driver: orm | |
# Add this option to let Vich use the FlySystem as a storage engine | |
storage: flysystem | |
mappings: | |
user_profile_picture: | |
# I used the URL of Bucket, including the prefix used in the flysystem.yaml file. | |
# This allowing me to resolve automatically the full URL of the image uploaded on the Google Cloud Storage | |
uri_prefix: https://storage.googleapis.com/bucket-name/some-prefix | |
# I used the name of the flystem storage I created on the flysystem.yaml | |
upload_destination: profile_pictures.storage | |
namer: Vich\UploaderBundle\Naming\SmartUniqueNamer | |
inject_on_load: false | |
delete_on_update: true | |
delete_on_remove: true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment