'Laravel Vapor file upload fails
Following the Laravel Vapor documentation for file uploads (https://docs.vapor.build/1.0/resources/storage.html#file-uploads), I'm encountering the following error when I try to upload the file from localhost to S3:
POST http://localhost:8000/vapor/signed-storage-url 500 (Internal Server Error)
The laravel log states the following:
Unable to issue signed URL. Missing environment variables: AWS_BUCKET, AWS_DEFAULT_REGION, AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY at Http\\Controllers\\SignedStorageUrlController.php:107
All of those environment variables are defined in the .env file.
Any ideas on how to overcome this issue?
Solution 1:[1]
Laravel Vapor uses the $_ENV array to read the environment variables. Mine was empty:
dd($_ENV) = []
After some research, I found out that php.ini has to be set to allow/enable PHP to set the $_ENV variables from -env. My local php-fpm Docker development setup had this disabled by default. I just had to update the variables_order = "EGPCS" to allow this to happen and then Vapor works correctly.
&& sed -E -i -e "s/variables_order.*/variables_order = \"EGPCS\"/g" "$PHP_INI_DIR/php.ini"
I'm using Docker, but I assume if you enable or amend this in a local php.ini file you can get this working also.
Solution 2:[2]
Set AWS_BUCKET, AWS_DEFAULT_REGION, AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY inside secrets area of Vapor dashboard or cli.
Solution 3:[3]
You need to update your env.production or whatever environment you are using
vapor env:pull production // or staging etc
Add those aws values in there then
vapor env:push production
Then deploy to your environment and your environment variables will be used. Your .env is only local
Solution 4:[4]
To use the S3 storage with vapor locally, you'll need to set the AWS environment variables into your .env.
These variables:
AWS_BUCKET
, AWS_DEFAULT_REGION
, AWS_ACCESS_KEY_ID
, AWS_SECRET_ACCESS_KEY
.
When running in the vapor, these variables are injected into your environment during execution time.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | alexmcfarlane |
Solution 2 | Jamie Ross |
Solution 3 | BobB |
Solution 4 | Rafael Laurindo |