2

I'm trying to use Localstack to mock an s3 instance so that I can upload images from a form. Here's my docker-compose:

localstack:
    image: localstack/localstack:latest
    container_name: localstack
    environment: 
      - AWS_DEFAULT_REGION=us-east-1
      - SERVICES=s3
      - DEBUG=1
      - DATA_DIR=/tmp/localstack/data
      - DOCKER_HOST=unix:///var/run/docker.sock      
    ports: 
      - "4566:4566"
    volumes: 
      - "${TEMPDIR:-/tmp/localstack}:/tmp/localstack"
      - "/var/run/docker.sock:/var/run/docker.sock"

My laravel filesystems.php looks like this:

's3' => [
            'driver' => 's3',
            'key' => env('AWS_ACCESS_KEY_ID'),
            'secret' => env('AWS_SECRET_ACCESS_KEY'),
            'region' => env('AWS_DEFAULT_REGION'),
            'bucket' => env('AWS_BUCKET'),
            'endpoint' => env('AWS_ENDPOINT'),
        ],

And my envfile:

AWS_ACCESS_KEY_ID=localstack
AWS_SECRET_ACCESS_KEY=localstack
AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=catering-management
AWS_ENDPOINT=http://s3.localhost:4566

I tried 2 methods, the first one just saves on my local storage:

$path = Storage::putFileAs(
    '/'.auth()->user()->tenant_id, $request->file('media_name'),
    $request->file('media_name')->getClientOriginalName()
);

The second method, which seems the one that tries to connect to localstack is:

Storage::disk('s3')->put(auth()->user()->tenant_id.'/', $request->file('media_name'));

Only that it hangs until "Maximum execution time of 60 seconds exceeded" is thrown.

What am I doing wrong here? Any hint?

Thank you very much

EDIT:

So it looks like a problem with lavarel itself. Any HTTP call to any other service on localhost times out using GuzzleHttp client.

user3353167
  • 782
  • 2
  • 16
  • 31

1 Answers1

2

I finally found the solution. After setting up a nodejs server doing exactly the same thing, I quickly found out about the 's3ForcePathStyle' flag. Took me hours to find the equivalent in PHP. Just add the property:

'use_path_style_endpoint' => true

to filesystems.php.

All merits to https://stackoverflow.com/a/54826384/3353167

user3353167
  • 782
  • 2
  • 16
  • 31