Skip to content

Instantly share code, notes, and snippets.

@rossriley
Last active July 9, 2019 13:34
Show Gist options
  • Save rossriley/911b849a14717c3563d8 to your computer and use it in GitHub Desktop.
Save rossriley/911b849a14717c3563d8 to your computer and use it in GitHub Desktop.
Adding an S3 filesystem for Bolt
<?php
use Aws\S3\S3Client;
use League\Flysystem\Filesystem;
use League\Flysystem\Adapter\AwsS3 as Adapter;
$client = S3Client::factory(array(
'key' => '[your key]',
'secret' => '[your secret]',
'region' => '[aws-region]'
));
$adapter = new Adapter($client, 'bucket-name', 'optional-prefix');
$filesystem = new Filesystem($adapter);
// Then with Bolt App
$app['filesystem']->mountFilesystem('s3fs', $filesystem);
@romulo1984
Copy link

I made some adaptations to the code, and adapted it to the structure of an extension. I have not published it yet because it contains some errors yet, but I hope to make it available soon.

<?php

namespace Bolt\Extension\YourName\YourExtension;

use Bolt\Extension\SimpleExtension;
use Silex\Application;
use Aws\S3\S3Client;
use Bolt\Filesystem\Filesystem;
use League\Flysystem\AwsS3v3\AwsS3Adapter as Adapter;

class YourExtensionExtension extends SimpleExtension
{
  public function boot(Application $app)
  {
    parent::boot($app);

    $client = new S3Client([
      'credentials' => [
        'key'    => '[key]',
        'secret' => '[secret]'
      ],
      'region' => '[region]',
      'version' => 'latest'
    ]);

    $adapter = new Adapter($client, '[bucket]');
    $filesystem = new Filesystem($adapter);
    // Then with Bolt App
    $app['filesystem']->mountFilesystem('files', $filesystem);
  }
}

If you are working with a local extension, be sure to install the dependencies with the composer in the project root.

The dependencies are:

composer require aws/aws-sdk-php league/flysystem league/flysystem-aws-s3-v3

@aberrabaa
Copy link

Hello,

I use an old version of BOLT (v2.0.5).
@romulo1984 : Does your comment above would work for this version, i need to implement an S3FileSystem and i have to be sure that an extension would be ok for my version.

Thanks a lot
Aymen

@romulo1984
Copy link

Hello @aberrabaa.

I do not know if the code would work in such an old version. We are already in version 3.6.9, and the 3.0.0 was released there in 2016, already with S3 support.

I suggest taking the time to study the migration of your code.

@aberrabaa
Copy link

You're completely right @romulo1984 and i propone the migration, but they don't accept to make it on summer cause of roadmap. This project will be plan on several month and meanwhile i have to enable managing filesystem with S3, no choice.

I am on php5.6, Bolt 2.05, and i tryed to create an extension as you propone above.
When you tested this way, did it work with the extension ?

@romulo1984
Copy link

I understand. My tests with that code there were in May 2017, so I really do not remember. But I believe it will not be so difficult. Just create this extension, register and see if everything is going as expected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment