Noah S
Noah S

Reputation: 63

Laravel Storage for Amazon S3 'put' method uses wrong URL

I am developing an app using Laravel 5.7 and currently trying to implement image storage using Amazon S3. I am using the league/flysystem-aws-s3-v3 package with Laravel's built in Storage facade. When I try to upload an image, the Storage->put() command is generating the wrong URL.

I have followed multiple tutorials and docs online and all of them just use the following command

Storage::disk('s3')->put('{file_name}', '{file_content}')

and the file_name is automatically prefixed with the amazon s3 url; however, for me this is not the case. But if I hardcode in the AWS_URL, everything works fine. I have all the correct values set up in the .env file (I believe) but Laravel seems to not be getting the AWS_URL correctly.

I have tried it with and without quotes around the env values. I have also ran

php artisan config:cache
php artisan cache:clear

to clear the env variables' cache.

.env

AWS_ACCESS_KEY_ID="**************"
AWS_SECRET_ACCESS_KEY="****************"
AWS_DEFAULT_REGION="us-east-1"
AWS_BUCKET="*****"
AWS_URL=http://******.s3.us-east-1.amazonaws.com

filesystems.php

'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'),
    ],

Using php artisan tinker:

This uploads the file properly, but I shouldn't need to hard code the url.

>>> Storage::disk('s3')->put('snoflow.s3.us-east-1.amazonaws.com/Hello.txt', 'Hello World!');
=> true`

This is how the command should be called but is giving an error. Notice the attempted Url is only the filename.

>>> Storage::disk('s3')->put('Hello.txt', 'Hello World!');
=> Aws/S3/Exception/S3Exception with message 'Error executing "PutObject" on "https://filename.txt"; AWS HTTP error: cURL error 6: Could not resolve host: filename.txt (see http://curl.haxx.se/libcurl/c/libcurl-errors.html)'`

Any help would be appreciated.

Upvotes: 5

Views: 6196

Answers (3)

nottherealironman
nottherealironman

Reputation: 389

Updating the .env to only use following variables as mentioned on this article worked for me:

AWS_ACCESS_KEY_ID=*****
AWS_SECRET_ACCESS_KEY=*****
AWS_DEFAULT_REGION=*****
AWS_BUCKET=*****
AWS_USE_PATH_STYLE_ENDPOINT=false  

Upvotes: 1

Nurkartiko
Nurkartiko

Reputation: 181

I got the same error, it looks like AWS HTTP not properly built. And solve by modify .env

Before:

AWS_ACCESS_KEY_ID=*****
AWS_SECRET_ACCESS_KEY=*****
AWS_DEFAULT_REGION=*****
AWS_BUCKET=*****
AWS_URL=
AWS_ENDPOINT=
AWS_USE_PATH_STYLE_ENDPOINT=false

After:

AWS_ACCESS_KEY_ID=*****
AWS_SECRET_ACCESS_KEY=*****
AWS_DEFAULT_REGION=*****
AWS_BUCKET=*****

Upvotes: 0

Rizky Arlin
Rizky Arlin

Reputation: 383

I solved it by removing the AWS_URL key in the .env.

Upvotes: 2

Related Questions