Search code examples
amazon-web-servicesamazon-s3magento2php-7.4localstack

How to use localstack as S3 in Magento 2 in development env?


I am trying to mock S3 on my local machine, for which I have used localstack. I am using localstack via docker image, which is running successfully on my local machine.

I have followed below URL to setup localstack in my system:

https://onexlab-io.medium.com/localstack-s3-e28ad393c09

I can create S3 bucket and upload and list files from my local machine after setup.

But how to use localstack in Magento 2 in development env. After setting up Magento and localstack whenever I am trying to enable remote storage via below command:

bin/magento setup:config:set --remote-storage-driver aws-s3 --remote-storage-bucket m242_bucket --remote-storage-region us-east-1

it is returning with below error message:

Adapter error: Unable to write file at location: storage.flag. In ConfigSetCommand.php line 151:
            Parameter validation failed

I think this error is due to endpoint URL of S3. We need to set localstack's S3 endpoint here in order to enable remote storage. But I am unable to get any help on google regarding this.

Please someone help me in this.


Solution

  • Cross-posted here

    Add this to the end of your env.php file just before the closing ];. This assumes

    ,
    'remote_storage' => [
            'driver' => 'aws-s3',
            'config' => [
                'bucket' => 'm242_bucket',
                'bucket_endpoint' => false,
                'region' => 'east-1',
                'scheme' => 'http',
                'endpoint' => 'http://localstack:4566',
                'use_path_style_endpoint' => true,
                'credentials' => [
                    'key' => 'C****************C',
                    'secret' => 'R****************2',
                ],
                'debug' => true,