0
votes

I'm trying to upload files to AWS S3 bucket. Here is my form and controller

<form action="{{ url('/') }}" method="post" enctype="multipart/form-data">
{{ csrf_field() }}
   <input type="file" name="image" id="image">
   <button type="submit">Save</button>
</form>
$file = $request->file('image');
$name = 'imgname.jpg';
$filePath = 'images/' . $name;
Storage::disk('s3')->put($filePath, file_get_contents($file));

Also I added AWS Credentials in .env file.

Laravel version 6.0

I got following error when I upload file.

GuzzleHttp\Exception\ConnectException cURL error 28: (see http://curl.haxx.se/libcurl/c/libcurl-errors.html)

2

2 Answers

3
votes

From cURL docs

CURLE_OPERATION_TIMEDOUT (28)

Operation timeout. The specified time-out period was reached according to the conditions.

So this is due to a network issue, you can change the options in config/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'),
    'curl.options' => [
        CURLOPT_CONNECTTIMEOUT => 5,
        CURLOPT_TIMEOUT => 10,
    ]
],

Of course, this doesn't have to be global, you can set the options on specific calls

Storage::disk('s3')->getDriver()->put($filePath, file_get_contents($file), [ 'curl.options' => [CURLOPT_CONNECTTIMEOUT => 5, CURLOPT_TIMEOUT => 10]]);

Hope this helps

1
votes

I fixed the issue. Issue was AWS Credentials not reading from .env file. So I directly added in filesystem.php file.

        's3' => [
            'driver' => 's3',
            'key' => 'KxxxxxxxN',
            'secret' => 'fSFrxxxxxxxxxx',
            'region' => 'us-east-1',
            'bucket' => 'bucket-name',
            'url' => 'http://s3.us-east-1.amazonaws.com/bucket-name',
        ],