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.