Skip to content
Advertisement

Laravel can’t connect to AWS S3

I have set up an S3 bucket and created an IAM user with full S3 access permission, and ran composer require league/flysystem-aws-s3-v3.

I have also have configured the following in .env:

JavaScript

The problem is that I can’t interact with S3 at all from my controller. I’ve tried sending files to S3:

JavaScript

I have also manually uploaded an image to S3, then tried to check if it can find it:

JavaScript

This results in this error: Unable to check existence for: photo.jpg

This makes me think that the issue is with flysystem-aws-s3-v3.

Is there a way to narrow down where the issue resides? by the way, I’m using Laravel 9 and flysystem-aws-s3-v3 3.0 if that helps.

Advertisement

Answer

I finally found the reason why the connection didn’t work. This is how the .env file looked like:

JavaScript

It turns out that Laravel (or maybe it was Flysystem?) already configured the same S3 variables and left the values empty. So my environment variables were getting overridden by these empty ones, leaving the connection to fail. I guess the lesson to learn here is to always check your entire .env file if it contains multiple vars with the same key.

Bonus

After setting up S3 with Laravel, you could use these snippets to:

  • Save an image to your S3 bucket:
JavaScript
  • Get an image from your S3 bucket:
JavaScript
  • Check if an image exists in your S3 bucket:
JavaScript
  • Delete an image from your S3 bucket:
JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement