简体   繁体   中英

Laravel 9 accessing AWS S3 - UnableToWriteFile at location

I have built a CRUD application with Laravel, but as I want it to be hosted on heroku, using the Laravel storage is not a solution, which is why I am trying to use AWS S3.

I followed a few tutorials, and it seemed pretty straight forward to do, however I get this error : League \ Flysystem \ UnableToWriteFile Unable to write file at location: nameOfMyPicture.jpg.

What I have done so far :

I have run the command composer require league/flysystem-aws-s3-v3. I have run config:clear and cache:clear. I have run multiple times composer update.

My .env file should have the right information as well. I tried not using .env file and putting all the information directly in the filesystems.php file.

Here is the code in my query.

 if ($request->hasfile('photo')) {
            $file = $request->file('photo');

            $name = time() . '.' . $file->getClientOriginalExtension();
            $filePath = 'locations/' . $name;

            Storage::disk('s3'))->put($filePath, file_get_contents($file);

            return back()->with('msg', 'Image Uploaded successfully');
        }

When using

if (Storage::disk('s3')->exists('my-file-name.jpg')) {
            dd("hello");
        }

I get the error message : Unable to check existence for: myFileName.

When I dd(Storage::disk('s3'))->put($filePath, file_get_contents($file)); , the url is null, but I do get the right bucket name, the credentials from my env file, the right region etc... Here is the dd.

^ Illuminate\Filesystem\AwsS3V3Adapter {#1388 ▼
  #driver: League\Flysystem\Filesystem {#1379 ▼
    -adapter: League\Flysystem\AwsS3V3\AwsS3V3Adapter {#1382 ▶}
    -config: League\Flysystem\Config {#1378 ▼
      -options: array:1 [▼
        "url" => null
      ]
    }
    -pathNormalizer: League\Flysystem\WhitespacePathNormalizer {#1374}
  }
  #adapter: League\Flysystem\AwsS3V3\AwsS3V3Adapter {#1382 ▼
    -client: Aws\S3\S3Client {#1343 ▼
      -aliases: null
      -config: array:9 [▶]
      -region: "eu-west-3"
      -endpoint: GuzzleHttp\Psr7\Uri {#1450 ▼
        -scheme: "https"
        -userInfo: ""
        -host: "s3.eu-west-3.amazonaws.com"
        -port: null
        -path: ""
        -query: ""
        -fragment: ""
        -composedComponents: null
      }
      -api: Aws\Api\Service {#1355 ▼
        #definition: array:4 [▶]
        #shapeMap: Aws\Api\ShapeMap {#1356 ▶}
        -apiProvider: Aws\Api\ApiProvider {#1354 ▶}
        -serviceName: "s3"
        -apiVersion: "2006-03-01"
        -operations: []
        -paginators: null
        -waiters: null
      }
      -signatureProvider: Closure($version, $service, $region) {#1353 ▶}
      -credentialProvider: Closure() {#1401 ▶}
      -handlerList: Aws\HandlerList {#1350 ▶}
      -defaultRequestOptions: []
    }
    -prefixer: League\Flysystem\PathPrefixer {#1376 ▶}
    -bucket: "veville-images"
    -visibility: League\Flysystem\AwsS3V3\PortableVisibilityConverter {#1339 ▶}
    -mimeTypeDetector: League\MimeTypeDetection\FinfoMimeTypeDetector {#1381 ▶}
    -options: []
    -streamReads: false
  }
  #config: array:11 [▼
    "driver" => "s3"
    "key" => "keyFromEnvFile"
    "secret" => "keyFromEnvFile"
    "region" => "eu-west-3"
    "bucket" => "veville-images"
    "url" => null
    "endpoint" => null
    "use_path_style_endpoint" => false
    "throw" => true
    "version" => "latest"
    "credentials" => array:2 [▼
      "key" => "keyFromEnvFile"
      "secret" => "keyFromEnvFile"
    ]
  ]
  #prefixer: League\Flysystem\PathPrefixer {#1377 ▶}
  #temporaryUrlCallback: null
  #client: Aws\S3\S3Client {#1343 ▶}

The filesystems.php file concerning s3

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

My .env file

AWS_ACCESS_KEY_ID=rightkey
AWS_SECRET_ACCESS_KEY=rightsecret
AWS_DEFAULT_REGION=eu-west-3
AWS_BUCKET=veville-images
AWS_USE_PATH_STYLE_ENDPOINT=false

Here is the AWS User Permission policies : user_policy .

When using the terminal, using the same AWS credentials, I have access to my bucket, and can upload and download files. However I can't through Laravel. I am at a complete loss here. Any help would be greatly appreciated.

Thank you in advance.

To anyone who might have the same problem. I managed to upload a file to my s3 bucket.

After looking around on the github, there was a commit not merged that would give the actual error which was : 'Error executing "PutObject" on "https://bucket.s3.region.amazonaws.com/file.ext"; AWS HTTP error: cURL error 60: SSL certificate problem: unable to get local issuer certificate (see https://curl.haxx.se/libcurl/c/libcurl-errors.html ) for https://bucket.s3.region.amazonaws.com/file.ext'

To resolve that issue : I have a very Simple Solution to this problem. You can do this without any certificate file.

Go on Laravel Root Folder -> Vender -> guzzlehttp -> guzzle -> src

open Client.php

find $defaults Array . that looks like this way.

$defaults = [
    'allow_redirects' => RedirectMiddleware::$defaultSettings,
    'http_errors'     => true,
    'decode_content'  => true,
    'verify'          => true,
    'cookies'         => false
];

Now main Job is to change value of verify key.

'verify'          => false,

So After this, it will not check SSL Certificate for CURL Request. This Solution works for me. I find this solution after much research.

Note: 'verify' => false can create a security issue in any Live or Development server. Do not try this on Server. This solution is only for Local System. Answer found here : AWS SSL security error : [curl] 60: SSL certificate prob...: unable to get local issuer certificate

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM