localstack s3 with laravel






first clone

Terminal:
git clone https://github.com/localstack/localstack.git

cd and reset to commit
Terminal:
cd localstack && git reset --hard cd3efec


run with docker-compose
Terminal:
sudo docker-compose up

install aws-cli
Terminal:
pip instasll awscli


create bucket
Terminal:
export AWS_ACCESS_KEY_ID=foo && export AWS_SECRET_ACCESS_KEY=foo  && aws --endpoint-url=http://localhost:4572/ s3 mb s3://mybucket $@

you can see the bucket created tohttp://localhost:8080/#!/infra










add credentials to .env laravel
Terminal:
AWS_ACCESS_KEY_ID=foo
AWS_SECRET_ACCESS_KEY=foo
AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=mybucket
AWS_URL=http://localhost:4572/



add new provider
\App\Providers\AwsS3ServiceProvider
Terminal:
<?php

namespace App\Providers;

use Illuminate\Support\Facades\Storage;
use Illuminate\Support\ServiceProvider;
use Aws\S3\S3Client;
use League\Flysystem\AwsS3v3\AwsS3Adapter;
use League\Flysystem\Filesystem;

/**
 * Class AwsS3ServiceProvider
 * @package App\Providers
 */
class AwsS3ServiceProvider extends ServiceProvider
{

    /**
     * Perform post-registration booting of services.
     *
     * @return void
     */
    public function boot()
    {

        Storage::extend('s3', function ($app, $config) {

            $args = [
                'credentials' => [
                    'key'    => $config[ 'key' ],
                    'secret' => $config[ 'secret' ],
                ],
                'version'     => 'latest',
                'region'      => $config[ 'region' ],
                'endpoint'    => $config[ 'url' ],
            ];
            if ( !empty($config[ 'url' ])) {
                $args[ 'url' ] = $config[ 'url' ];
            }
            $client = new S3Client($args);

            return new Filesystem(new AwsS3Adapter($client, $config[ 'bucket' ]));
        });
    }

    /**
     * Register bindings in the container.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}





add provider to contig/app.php
Terminal:
App\Providers\AwsS3ServiceProvider::class,






0 comentarios :