Skip to content

Instantly share code, notes, and snippets.

@normeno
Last active July 24, 2021 22:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save normeno/9819d3d7f69db6d5de1177bfa74081e5 to your computer and use it in GitHub Desktop.
Save normeno/9819d3d7f69db6d5de1177bfa74081e5 to your computer and use it in GitHub Desktop.
Lumen filesystem sample config - Base on Yantb Tutorial

based on Yantb - Trabajar archivos en Lumen

Cuando queremos trabajar archivos en Lumen, nos damos cuenta que es un poco más complejo que al hacerlo en Laravel. Esto es debido a que Lumen no trae pre-instalada las dependencias y configuraciones necesarias, no obstante, al incluirlas, podemos trabajar los archivos de la misma forma en que lo hacemos en Laravel.

Lo primero es configurar nuestro archivo .env con las variables necesarias

AWS_KEY=
AWS_SECRET=
AWS_REGION=
AWS_BUCKET=

Ahora instalamos las dependencias que no nos provee Lumen

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

Lo próximo a realizar es crear nuestro archivo /config/filesystems.php, el cual es una copia de Laravel FileSystem

<?php

return [

    /*
    |--------------------------------------------------------------------------
    | Default Filesystem Disk
    |--------------------------------------------------------------------------
    |
    | Here you may specify the default filesystem disk that should be used
    | by the framework. The "local" disk, as well as a variety of cloud
    | based disks are available to your application. Just store away!
    |
    */

    'default' => env('FILESYSTEM_DRIVER', 'local'),

    /*
    |--------------------------------------------------------------------------
    | Default Cloud Filesystem Disk
    |--------------------------------------------------------------------------
    |
    | Many applications store files both locally and in the cloud. For this
    | reason, you may specify a default "cloud" driver here. This driver
    | will be bound as the Cloud disk implementation in the container.
    |
    */

    'cloud' => env('FILESYSTEM_CLOUD', 's3'),

    /*
    |--------------------------------------------------------------------------
    | Filesystem Disks
    |--------------------------------------------------------------------------
    |
    | Here you may configure as many filesystem "disks" as you wish, and you
    | may even configure multiple disks of the same driver. Defaults have
    | been setup for each driver as an example of the required options.
    |
    | Supported Drivers: "local", "ftp", "s3", "rackspace"
    |
    */

    'disks' => [

        'local' => [
            'driver' => 'local',
            'root' => storage_path('app'),
        ],

        'public' => [
            'driver' => 'local',
            'root' => storage_path('app/public'),
            'url' => env('APP_URL').'/storage',
            'visibility' => 'public',
        ],

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

    ],

];

La siguiente modificación a realizar es en el archivo /bootstrap/app.php. Lo primero es que en la sección de Register Container Bindings agregamos

$app->singleton('filesystem', function ($app) {
    return $app->loadComponent(
        'filesystems',
        Illuminate\Filesystem\FilesystemServiceProvider::class,
        'filesystem'
    );
});

En el mismo archivo, buscamos Register Service Providers y agregamos

$app->configure('filesystems');
$app->register(Illuminate\Filesystem\FilesystemServiceProvider::class);

Con todo esto, podemos decir que ya tenemos la base, que es las dependencias y configuraciones necesarias. Ahora podemos trabajar de forma normal, por ejemplo, si queremos subir un archivo a AWS S3.

$s3 = Storage::disk('s3');
$s3->put('README.md', file_get_contents(base_path('README.md')), 'public');

Lo que hace este ejemplo, es indicar que trabajaremos con S3 e indicar que vamos a subir nuestro README.md con el mismo nombre y de forma pública.

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