Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

rktaxali's avatar

Problem storing a file in AWS S3

In my Laravel 10 project, I am getting an error when I try to upload a file to my AWS S3 bucket using PutObject() I am using league/flysystem-aws-s3-v3 version 3.16. My .env configuration:

AWS_ACCESS_KEY_ID=AKIA23ECJXS3U3M****
AWS_SECRET_ACCESS_KEY=DnB8dkbIenoVeTOWVgBPi*******

AWS_DEFAULT_REGION=ca-central-1
AWS_BUCKET=silkweb-canada
AWS_URL=http://silkweb-canada.s3.ca-central-1.amazonaws.com
AWS_ENDPOINT=http://silkweb-canada.s3.ca-central-1.amazonaws.com
AWS_USE_PATH_STYLE_ENDPOINT=false
AWS_S3_SILKWEB_URL="https://silkweb-canada.s3.ca-central-1.amazonaws.com/"

My S3 configuration in 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'),
            'endpoint' => env('AWS_ENDPOINT'),
            'use_path_style_endpoint' => env('AWS_USE_PATH_STYLE_ENDPOINT', false),
            'throw' => true,
        ],

When I upload a file using PutObject(), I get an error message:

<Error><Code>NoSuchBucket</Code><Message>The specified bucket does not exist</Mes (truncated...) NoSuchBucket (client): The specified bucket does not exist - <Error><Code>NoSuchBucket</Code><Message>The specified bucket does not exist</Message><BucketName>silkweb-canada.silkweb-canada</BucketName><RequestId>GSH4FZAJ51ZHXZ7W</RequestId><HostId>VYDGFBI1WRemHbgcfx6f4CRdcY1pwRr/kAwkupClBIhp5uu/ARRu+wchhMjAfWbiGu9RQDsavJI=</HostId></Error> at /var/www/vhosts/app.silkweb.ca/vendor/aws/aws-sdk-php/src/WrappedHttpHandler.php:196)

However, if I change ASW_URL and AWS_ENDPOINT from http to https, the error message changes to:

Unable to write file at location: client/temp/27. Error executing "PutObject" on "https://silkweb-canada.silkweb-canada.s3.ca-central-1.amazonaws.com/client/temp/27"; AWS HTTP error: cURL error 60: SSL: no alternative certificate subject name matches target host name 'silkweb-canada.silkweb-canada.s3.ca-central-1.amazonaws.com' (see https://curl.haxx.se/libcurl/c/libcurl-errors.html) for https://silkweb-canada.silkweb-canada.s3.ca-central-1.amazonaws.com/client/temp/27 {"userId":2,"exception":"[object] (League\Flysystem\UnableToWriteFile(code: 0): Unable to write file at location: client/temp/27. Error executing "PutObject" on "https://silkweb-canada.silkweb-canada.s3.ca-central-1.amazonaws.com/client/temp/27"; AWS HTTP error: cURL error 60: SSL: no alternative certificate subject name matches target host name 'silkweb-canada.silkweb-canada.s3.ca-central-1.amazonaws.com' (see https://curl.haxx.se/libcurl/c/libcurl-errors.html) for https://silkweb-canada.silkweb-canada.s3.ca-central-1.amazonaws.com/client/temp/27 at /var/www/vhosts/app.silkweb.ca/vendor/league/flysystem/src/UnableToWriteFile.php:24)

Notice that in this case, the bucket name appears twice in the url (https://silkweb-canada.silkweb-canada.s3.ca-central-1.amazonaws.com/client/temp/27) .

My first question: What is the syntax or correct procedure to define AWS_URL and AWS_ENDPOINT? and the second question: How to fix this issue?

Thanks

0 likes
5 replies
martinbean's avatar
Level 80

@rktaxali Remove the AWS_URL and AWS_ENDPOINT environment variables. Laravel (and Flysystem) will be able to build the necessary S3 URLs just fine from your bucket name and region.

1 like
FatimaEzzahraSeffari's avatar

i have a prb i can't write in the s3 bucket i work for a project using laravel and this is .env config;

AWS_ACCESS_KEY_ID=............................ AWS_SECRET_ACCESS_KEY=..................................... AWS_DEFAULT_REGION=ap-south-1 AWS_BUCKET=............................................. AWS_USE_PATH_STYLE_ENDPOINT=false and this is my filesysstems 's3' => [ 'driver' => 's3', 'key' => env('AWS_ACCESS_KEY_ID'), 'secret' => env('AWS_SECRET_ACCESS_KEY'), 'region' => env('AWS_DEFAULT_REGION'), 'bucket' => env('AWS_BUCKET'), 'use_path_style_endpoint' => env('AWS_USE_PATH_STYLE_ENDPOINT', false), 'throw' => true, ], after i fill my laravel form and submited the data stored in my table sql but in the bucket s3 nothing is here i don't know the prb where help me please

rktaxali's avatar

Can't make out much from the message you have posted. However, I can provide what is working for me:

In .env file:

AWS_ACCESS_KEY_ID=AKIA23ECJXS**********
AWS_SECRET_ACCESS_KEY=DnB8dk*****************

AWS_DEFAULT_REGION=ca-central-1
AWS_BUCKET=silkweb-bucket
AWS_USE_PATH_STYLE_ENDPOINT=false
AWS_S3_SILKWEB_URL="https://silkweb-bucket.s3.ca-central-1.amazonaws.com/"

In app.php file:

 'aws_s3_silkweb_url' => env('AWS_S3_SILKWEB_URL', ''),

And this is the code I use to store an image in S3 bucket:

// save in AWS s3
          $filepath = "client/profile/" . $client_id . '/' . uniqid(date('YmdHis')) . '-' .  $basename;
          $width =  config('app.clientProfileImageWidth');
          $image = Image::make($request->file('file'))->resize($width, null, function ($constraint) {
            $constraint->aspectRatio();
            $constraint->upsize();
          });    // keep original format

          $ok = Storage::disk('s3')->put($filepath, $image->stream());
          if ($ok) {
            $upload_path =  $filepath;
            $returnArray['upload_path'] =   $filepath;
            $returnArray['path'] = config('app.aws_S3_SILKWEB_URL') .   $filepath;
          }

          
        }

Please or to participate in this conversation.