laravel / framework

The Laravel Framework.
https://laravel.com
MIT License
32.54k stars 11.02k forks source link

AWS S3 - Missing required client configuration options #28070

Closed marlongichie closed 5 years ago

marlongichie commented 5 years ago

Description:

When trying to upload files to AWS S3, the following error is reported:

A "region" configuration value is required for the "s3" service
  (e.g., "us-west-2"). A list of available public regions and endpoints can be
  found at http://docs.aws.amazon.com/general/latest/gr/rande.html.  

Steps To Reproduce:

Configure environment variables as follows:

FILESYSTEM_DRIVER=s3
FILESYSTEM_CLOUD=s3

AWS_ACCESS_KEY_ID=XXXXXXXXXXXXXXX
AWS_SECRET_ACCESS_KEY=xxxxxxxxxxxxxxxxxxxxxxxxxxx
AWS_DEFAULT_REGION=us-east-2
AWS_BUCKET=bucketname
AWS_URL=https://apigateway.us-east-2.amazonaws.com

Has anyone else come across this issue? Am I missing something?

staudenmeir commented 5 years ago

Have you cached your configuration?

Does dd(config('filesystems.disks.s3.region')); show the correct value?

marlongichie commented 5 years ago

@staudenmeir No. dd(config('filesystems.disks.s3.region')); returns null

See below output of dd(config('filesystems.disks.s3'));

array:6 [▼
  "driver" => "s3"
  "key" => null
  "secret" => null
  "region" => null
  "bucket" => null
  "url" => null
]
staudenmeir commented 5 years ago

What does your config/filesystems.php file look like?

marlongichie commented 5 years ago

I'm using the default config/filesystems.php. See below:

<?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", "sftp", "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_ACCESS_KEY_ID'),
            'secret' => env('AWS_SECRET_ACCESS_KEY'),
            'region' => env('AWS_DEFAULT_REGION'),
            'bucket' => env('AWS_BUCKET'),
            'url' => env('AWS_URL'),
        ],

    ],

];
staudenmeir commented 5 years ago

Are your environment variables configured in the .env file?

What does dd(env('AWS_DEFAULT_REGION')); show you?

marlongichie commented 5 years ago

Snippet from .env file below APP_NAME="My App Name"

FILESYSTEM_DRIVER=s3 FILESYSTEM_CLOUD=s3

AWS_ACCESS_KEY_ID=XXXXXXXXXXXXXX AWS_SECRET_ACCESS_KEY=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx AWS_DEFAULT_REGION=us-east-2 AWS_BUCKET=bucketname AWS_URL=https://apigateway.us-east-2.amazonaws.com

dd(env('AWS_DEFAULT_REGION')) returns null

dd() for all variables associated with FILESYSTEM & AWS returns null

However other env variables return expected values: dd(env('APP_NAME')) returns My App Name also dd(config('app.name')) returns My App Name

staudenmeir commented 5 years ago

Can you reproduce this on a fresh installation of Laravel?

marlongichie commented 5 years ago

Did a fresh install of Laravel just now.

Configured the following:

FILESYSTEM_DRIVER=s3
FILESYSTEM_CLOUD=s3

AWS_ACCESS_KEY_ID=XXXXXXXXXXXXXX
AWS_SECRET_ACCESS_KEY=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
AWS_DEFAULT_REGION=us-east-2
AWS_BUCKET=bucketname
AWS_URL=https://apigateway.us-east-2.amazonaws.com

See below output of dd(config('filesystems.disks.s3'))

array:6 [▼
  "driver" => "s3"
  "key" => ""
  "secret" => ""
  "region" => "us-east-1"
  "bucket" => ""
  "url" => "https://apigateway.us-east-2.amazonaws.com"
]

Output from dd(env('AWS_DEFAULT_REGION')) is "us-east-1"

Looks like there is a default region that is overwriting the value I set in the .env file. Also, the other values return an empty string in this case.

Strange behavior. Your thoughts?

marlongichie commented 5 years ago

I was able to solve this in my existing project. It seems there was a missing closing quote in the .env file before the FILESYSTEM_ variables which affected everything that came after it.

MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER <--missing closing quote

FILESYSTEM_DRIVER=s3
FILESYSTEM_CLOUD=s3
...

But, I'm not sure what issue was causing the variable to be overwritten in the fresh Laravel install project.

Thanks @staudenmeir .

staudenmeir commented 5 years ago

Regarding the fresh installation: The sample .env file already contains default values for AWS_DEFAULT_REGION and others. Did you remove/replace those?

marlongichie commented 5 years ago

You're absolutely right @staudenmeir .

Looks to be configuration error rather than an issue with Laravel itself. I think it's fair to close this out at this point.

Thank you again for helping with this issue.

Best.

staudenmeir commented 5 years ago

Please close the issue.

dukesnuz commented 4 years ago

I know this is closed. i had same issue and different problem. I had copied the .env AWS configuration from one Laravel version to another version. Turns out the config/filesystem AWS variables were different from each Laravel version One version used AWS_DEFAULT_REGION= and Other used AWS_REGION=

Natfan commented 4 years ago

For reference my fix was to run php artisan config:cache, which wiped the current version of my .env cache and replaced it with the live values.

jessiesiat commented 4 years ago

if php artisan config:clear doesnt work.

It can be that you already have media in your database that points to s3, and when retrieve it tries to resolve the url. When you don't already have the s3 credentials or commented it out in .env because you switch to another disk, you will get that error.

I encountered that error using spatie/laravel-medialibrary

Fitmavincent commented 3 years ago

if php artisan config:clear doesnt work.

It can be that you already have media in your database that points to s3, and when retrieve it tries to resolve the url. When you don't already have the s3 credentials or commented it out in .env because you switch to another disk, you will get that error.

I encountered that error using spatie/laravel-medialibrary

Hi Jessie,

I'm using the spatie/laravel-medialibrary and I got the error when hosting the app on Laravel Forge. Checked every config from tinker and looks correct to me but getting missing configuration error regardless, which local is totally fine. Wondering how do you solve this problem? If there's something in DB point to S3, what tables I should check? Thank you.

Enaah commented 2 years ago

if php artisan config:clear doesnt work. It can be that you already have media in your database that points to s3, and when retrieve it tries to resolve the url. When you don't already have the s3 credentials or commented it out in .env because you switch to another disk, you will get that error. I encountered that error using spatie/laravel-medialibrary

Hi Jessie,

I'm using the spatie/laravel-medialibrary and I got the error when hosting the app on Laravel Forge. Checked every config from tinker and looks correct to me but getting missing configuration error regardless, which local is totally fine. Wondering how do you solve this problem? If there's something in DB point to S3, what tables I should check? Thank you.

Hey there @Fitmavincent,

I am running into this issue myself, could you solve it?