XF 2.1 Implementing Flysystem with GCS adapter

Zig

Member
Hi everyone,

I'm trying to implement XF's flysystem support (https://xenforo.com/community/resou...or-amazon-s3-for-file-storage-in-xf-2-x.6805/), but my company uses Google Cloud Storage, which isn't S3 compliant. Luckily, there is an available adapter for GCS (https://github.com/Superbalist/flysystem-google-cloud-storage).

Unfortunately, I'm having a lot of trouble getting XF to recognize the newly installed vendor code. My attempts to install it into XF's /src/vendor/ directory via composer require leads me into dependency hell (related to vendor extensions I don't need). I'm wondering... does anyone have experience adding vendor code to XF? Is there maybe a trick I'm missing here?

Thanks and all the best!
zig
 
There isn't an addon. I'm just trying to get the GCS adapter for Flysystem installed so that the closures I added to config.php (as described in the S3 guide) don't throw Class 'Superbalist\Flysystem\GoogleStorage\GoogleStorageAdapter' not found.

EDIT: There will be an addon to manipulate file paths to match our bucket naming scheme, but I'd like to show proof of concept that the adapter works before investing significant development time.
 
Last edited:
This is what I've added to config.php.

Code:
$storageClient = function()
{
    // Credentials not specified because the service account should automatically authenticate.
    return new \Google\Cloud\Storage\StorageClient([
        'projectId' => 'our-project-id-here'
    ]);
};

$config['fsAdapters']['data'] = function() use ($storageClient)
{
    return new \Superbalist\Flysystem\GoogleStorage\GoogleStorageAdapter(
        $storageClient,
        $storageClient->bucket('our-bucket-name-here'),
        'data'
    );
};

$config['fsAdapters']['internal-data'] = function() use ($storageClient)
{
    return new \Superbalist\Flysystem\GoogleStorage\GoogleStorageAdapter(
        $storageClient,
        $storageClient->bucket('our-bucket-name-here'),
        'internal_data'
    );
};

$config['externalDataUrl'] = function($externalPath, $canonical)
{
    return 'https://our-cdn-url-here/' . $externalPath;
};
 
I was able to resolve the dependency issues and get composer to install the Superbalist adapter to the vendor directory, but now I'm seeing a rather unexpected error:

Fatal error: Uncaught Error: Class '\XF\App' not found in /srv/www/src/XF.php:361 Stack trace: #0 /srv/www/src/XF.php(352): XF::setupApp('\\XF\\App') #1 /srv/www/src/XF.php(169): XF::app() #2 [internal function]: XF::handleException(Object(Error)) #3 {main} thrown in /srv/www/src/XF.php on line 361

o_O
 
I was able to resolve the dependency issues and get composer to install the Superbalist adapter to the vendor directory, but now I'm seeing a rather unexpected error:

Fatal error: Uncaught Error: Class '\XF\App' not found in /srv/www/src/XF.php:361 Stack trace: #0 /srv/www/src/XF.php(352): XF::setupApp('\\XF\\App') #1 /srv/www/src/XF.php(169): XF::app() #2 [internal function]: XF::handleException(Object(Error)) #3 {main} thrown in /srv/www/src/XF.php on line 361

o_O

That should be something else you did with your code. As far as I remember you need an add-on with a specific setting in the addon.json file to automatically load the vendor/autoload.php file to include your composer packages.

Here's the setting:

"composer_autoload": "vendor/composer"

If you're using XF2 or if it doesn't work for you, you need to include a special file in your config.php to do the autoloading for you. Checkout the AWS flysystem integration for XF2 (not 2.1) for an example.
 
  • Like
Reactions: Zig
That should be something else you did with your code. As far as I remember you need an add-on with a specific setting in the addon.json file to automatically load the vendor/autoload.php file to include your composer packages.

Here's the setting:

"composer_autoload": "vendor/composer"

If you're using XF2 or if it doesn't work for you, you need to include a special file in your config.php to do the autoloading for you. Checkout the AWS flysystem integration for XF2 (not 2.1) for an example.

Thanks so much! I'll give this a try today.
 
Just wanted to drop back in and say that autoloading through an addon has worked wonderfully. Thanks again, @JulianD!

If anyone else tries using Superbalist's GCS adapter, the config.php code above didn't quite work as intended. I was able to get it working by using this instead:

PHP:
$config['fsAdapters']['data'] = function()
{
    $client = new \Google\Cloud\Storage\StorageClient([ 'projectId' => 'your-project-id-here']);
    $bucket = $client->bucket('your-bucket-name-here');
    return new \Superbalist\Flysystem\GoogleStorage\GoogleStorageAdapter($client, $bucket, 'data');
};
 
Top Bottom