The PHP League Flysystem for "Spaces" object storage

September 4, 2017 348 views
PHP Frameworks PHP

I am trying to use https://github.com/thephpleague/flysystem-aws-s3-v3 adapter for use with digital ocean spaces as the storage provider as the documentation suggests the api matches amazon s3

Has anyone managed to get flysystem working in any form with digital ocean spaces? I cant seem to work out how to override the base url, the rest of the credentials seem to be passed through.

2 comments
  • 's3' => [
    'driver' => 's3',
    'key' => env('AWSKEY'), ===> I found this
    'secret' => env('AWS
    SECRET'), but ?
    'region' => env('AWSREGION'), but ?
    'bucket' => env('AWS
    BUCKET'), but ?
    ],
    Can you please help me with the configs

    with Thanks

  • Of course! first of all from your digital ocean dashboard go to the api tab. Then look at your token/keys, specifically the spaces access keys. When you create a new key or regenerate one you will see the secret, you MUST take note as that is the only time you will see it (without regenerating again invalidating any apps attached). So now you have the key (AWSKEY in .env) and the secret (AWSSECRET). At the moment the only region available is NYC3 so it just set AWSREGION to NY3. When you created the space you will have called it something, whatever you called the space this will be the bucket name set AWSBUCKET to the name of the space. Finally the url was AWS_URL=https://nyc3.digitaloceanspaces.com.

    Set all of these in your .env and then add an entry in your filesystems.php as below

    'DO' => [
        'driver' => 's3',
        'key' => env('AWS_KEY'),
        'secret' => env('AWS_SECRET'),
        'region' => env('AWS_REGION'),
        'bucket' => env('AWS_BUCKET'),
        'endpoint' => env('AWS_URL')
    ],
    

    You can then use the storage facade as follows

    $disk = Storage::disk('DO');
    $content = filegetcontents($file);
    $this->storage->put($filePath, $content , 'public');

    Where file is a posted file from a form.

1 Answer
mikebywater September 5, 2017
Accepted Answer

This was actually very simple simply set the 'endpoint' in your filesystem config to the FQDN of DO spaces eg. in filesytems.php

    'DO' => [
        'driver' => 's3',
        'key' => env('AWS_KEY'),
        'secret' => env('AWS_SECRET'),
        'region' => env('AWS_REGION'),
        'bucket' => env('AWS_BUCKET'),
        'endpoint' => env('AWS_URL')
    ],

and in your .env

AWS_URL=https://nyc3.digitaloceanspaces.com

Have another answer? Share your knowledge.