I'm trying to use the AWS S3 service to store images, for now, on a local server. I followed this IAM and bucket configuration tutorial: "https://laravel-news.com/using-aws-s3-for-laravel-storage". The only difference is that I'm using an image sent via API to test the storage.
The following error is being triggered:
League\Flysystem\UnableToWriteFile: Unable to write file at location: U7bTIOhF5U0roIgHkVKcdE31XGgw7sE63xHHSx91.png. in file C:\Users\Flip_\Documents\GitHub\sistema_autopecas\vendor\league\flysystem\src\UnableToWriteFile.php on line 24
#0 C:\Users\Flip_\Documents\GitHub\sistema_autopecas\vendor\league\flysystem-aws-s3-v3\AwsS3V3Adapter.php(172): League\Flysystem\UnableToWriteFile::atLocation('U7bTIOhF5U0roIg...', '', Object(Aws\S3\Exception\S3Exception))
#1 C:\Users\Flip_\Documents\GitHub\sistema_autopecas\vendor\league\flysystem-aws-s3-v3\AwsS3V3Adapter.php(200): League\Flysystem\AwsS3V3\AwsS3V3Adapter->upload('U7bTIOhF5U0roIg...', Resource id #670, Object(League\Flysystem\Config))
#2 C:\Users\Flip_\Documents\GitHub\sistema_autopecas\vendor\league\flysystem\src\Filesystem.php(70): League\Flysystem\AwsS3V3\AwsS3V3Adapter->writeStream('U7bTIOhF5U0roIg...', Resource id #670, Object(League\Flysystem\Config))
#3 C:\Users\Flip_\Documents\GitHub\sistema_autopecas\vendor\laravel\framework\src\Illuminate\Filesystem\FilesystemAdapter.php(360): League\Flysystem\Filesystem->writeStream('U7bTIOhF5U0roIg...', Resource id #670, Array)
#4 C:\Users\Flip_\Documents\GitHub\sistema_autopecas\vendor\laravel\framework\src\Illuminate\Filesystem\FilesystemAdapter.php(403): Illuminate\Filesystem\FilesystemAdapter->put('U7bTIOhF5U0roIg...', Resource id #670, Array)
#5 C:\Users\Flip_\Documents\GitHub\sistema_autopecas\vendor\laravel\framework\src\Illuminate\Filesystem\FilesystemAdapter.php(383): Illuminate\Filesystem\FilesystemAdapter->putFileAs('U7bTIOhF5U0roIg...', Object(Illuminate\Http\UploadedFile), 'U7bTIOhF5U0roIg...', Array)
#6 C:\Users\Flip_\Documents\GitHub\sistema_autopecas\vendor\laravel\framework\src\Illuminate\Filesystem\FilesystemAdapter.php(349): Illuminate\Filesystem\FilesystemAdapter->putFile('', Object(Illuminate\Http\UploadedFile), Array)
#7 C:\Users\Flip_\Documents\GitHub\sistema_autopecas\app\Http\Controllers\ImageController.php(20): Illuminate\Filesystem\FilesystemAdapter->put('', Object(Illuminate\Http\UploadedFile))
#8 C:\Users\Flip_\Documents\GitHub\sistema_autopecas\vendor\laravel\framework\src\Illuminate\Routing\Controller.php(54): App\Http\Controllers\ImageController->upload(Object(Illuminate\Http\Request))
#9 C:\Users\Flip_\Documents\GitHub\sistema_autopecas\vendor\laravel\framework\src\Illuminate\Routing\ControllerDispatcher.php(43): Illuminate\Routing\Controller->callAction('upload', Array)
#10 C:\Users\Flip_\Documents\GitHub\sistema_autopecas\vendor\laravel\framework\src\Illuminate\Routing\Route.php(260): Illuminate\Routing\ControllerDispatcher->dispatch(Object(Illuminate\Routing\Route), Object(App\Http\Controllers\ImageController), 'upload')
#11 C:\Users\Flip_\Documents\GitHub\sistema_autopecas\vendor\laravel\framework\src\Illuminate\Routing\Route.php(205): Illuminate\Routing\Route->runController()
#12 C:\Users\Flip_\Documents\GitHub\sistema_autopecas\vendor\laravel\framework\src\Illuminate\Routing\Router.php(798): Illuminate\Routing\Route->run()
#13 C:\Users\Flip_\Documents\GitHub\sistema_autopecas\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(141): Illuminate\Routing\Router->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#14 C:\Users\Flip_\Documents\GitHub\sistema_autopecas\vendor\laravel\framework\src\Illuminate\Routing\Middleware\SubstituteBindings.php(50): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#15 C:\Users\Flip_\Documents\GitHub\sistema_autopecas\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(180): Illuminate\Routing\Middleware\SubstituteBindings->handle(Object(Illuminate\Http\Request), Object(Closure))
#16 C:\Users\Flip_\Documents\GitHub\sistema_autopecas\vendor\laravel\framework\src\Illuminate\Routing\Middleware\ThrottleRequests.php(126): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#17 C:\Users\Flip_\Documents\GitHub\sistema_autopecas\vendor\laravel\framework\src\Illuminate\Routing\Middleware\ThrottleRequests.php(102): Illuminate\Routing\Middleware\ThrottleRequests->handleRequest(Object(Illuminate\Http\Request), Object(Closure), Array)
#18 C:\Users\Flip_\Documents\GitHub\sistema_autopecas\vendor\laravel\framework\src\Illuminate\Routing\Middleware\ThrottleRequests.php(54): Illuminate\Routing\Middleware\ThrottleRequests->handleRequestUsingNamedLimiter(Object(Illuminate\Http\Request), Object(Closure), 'api', Object(Closure))
#19 C:\Users\Flip_\Documents\GitHub\sistema_autopecas\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(180): Illuminate\Routing\Middleware\ThrottleRequests->handle(Object(Illuminate\Http\Request), Object(Closure), 'api')
#20 C:\Users\Flip_\Documents\GitHub\sistema_autopecas\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(116): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#21 C:\Users\Flip_\Documents\GitHub\sistema_autopecas\vendor\laravel\framework\src\Illuminate\Routing\Router.php(799): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#22 C:\Users\Flip_\Documents\GitHub\sistema_autopecas\vendor\laravel\framework\src\Illuminate\Routing\Router.php(776): Illuminate\Routing\Router->runRouteWithinStack(Object(Illuminate\Routing\Route), Object(Illuminate\Http\Request))
#23 C:\Users\Flip_\Documents\GitHub\sistema_autopecas\vendor\laravel\framework\src\Illuminate\Routing\Router.php(740): Illuminate\Routing\Router->runRoute(Object(Illuminate\Http\Request), Object(Illuminate\Routing\Route))
#24 C:\Users\Flip_\Documents\GitHub\sistema_autopecas\vendor\laravel\framework\src\Illuminate\Routing\Router.php(729): Illuminate\Routing\Router->dispatchToRoute(Object(Illuminate\Http\Request))
#25 C:\Users\Flip_\Documents\GitHub\sistema_autopecas\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php(190): Illuminate\Routing\Router->dispatch(Object(Illuminate\Http\Request))
#26 C:\Users\Flip_\Documents\GitHub\sistema_autopecas\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(141): Illuminate\Foundation\Http\Kernel->Illuminate\Foundation\Http\{closure}(Object(Illuminate\Http\Request))
#27 C:\Users\Flip_\Documents\GitHub\sistema_autopecas\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\TransformsRequest.php(21): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#28 C:\Users\Flip_\Documents\GitHub\sistema_autopecas\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull.php(31): Illuminate\Foundation\Http\Middleware\TransformsRequest->handle(Object(Illuminate\Http\Request), Object(Closure))
#29 C:\Users\Flip_\Documents\GitHub\sistema_autopecas\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(180): Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull->handle(Object(Illuminate\Http\Request), Object(Closure))
#30 C:\Users\Flip_\Documents\GitHub\sistema_autopecas\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\TransformsRequest.php(21): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#31 C:\Users\Flip_\Documents\GitHub\sistema_autopecas\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\TrimStrings.php(40): Illuminate\Foundation\Http\Middleware\TransformsRequest->handle(Object(Illuminate\Http\Request), Object(Closure))
#32 C:\Users\Flip_\Documents\GitHub\sistema_autopecas\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(180): Illuminate\Foundation\Http\Middleware\TrimStrings->handle(Object(Illuminate\Http\Request), Object(Closure))
#33 C:\Users\Flip_\Documents\GitHub\sistema_autopecas\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\ValidatePostSize.php(27): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#34 C:\Users\Flip_\Documents\GitHub\sistema_autopecas\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(180): Illuminate\Foundation\Http\Middleware\ValidatePostSize->handle(Object(Illuminate\Http\Request), Object(Closure))
#35 C:\Users\Flip_\Documents\GitHub\sistema_autopecas\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\PreventRequestsDuringMaintenance.php(86): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#36 C:\Users\Flip_\Documents\GitHub\sistema_autopecas\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(180): Illuminate\Foundation\Http\Middleware\PreventRequestsDuringMaintenance->handle(Object(Illuminate\Http\Request), Object(Closure))
#37 C:\Users\Flip_\Documents\GitHub\sistema_autopecas\vendor\laravel\framework\src\Illuminate\Http\Middleware\HandleCors.php(62): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#38 C:\Users\Flip_\Documents\GitHub\sistema_autopecas\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(180): Illuminate\Http\Middleware\HandleCors->handle(Object(Illuminate\Http\Request), Object(Closure))
#39 C:\Users\Flip_\Documents\GitHub\sistema_autopecas\vendor\laravel\framework\src\Illuminate\Http\Middleware\TrustProxies.php(39): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#40 C:\Users\Flip_\Documents\GitHub\sistema_autopecas\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(180): Illuminate\Http\Middleware\TrustProxies->handle(Object(Illuminate\Http\Request), Object(Closure))
#41 C:\Users\Flip_\Documents\GitHub\sistema_autopecas\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(116): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#42 C:\Users\Flip_\Documents\GitHub\sistema_autopecas\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php(165): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#43 C:\Users\Flip_\Documents\GitHub\sistema_autopecas\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php(134): Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter(Object(Illuminate\Http\Request))
#44 C:\Users\Flip_\Documents\GitHub\sistema_autopecas\public\index.php(52): Illuminate\Foundation\Http\Kernel->handle(Object(Illuminate\Http\Request))
#45 C:\Users\Flip_\Documents\GitHub\sistema_autopecas\vendor\laravel\framework\src\Illuminate\Foundation\resources\server.php(16): require_once('C:\\Users\\Flip_\\...')
#46 {main}
My config/filesystem.php configuration is like this:
'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'),
'visibility' => 'public',
'use_path_style_endpoint' => env('AWS_USE_PATH_STYLE_ENDPOINT', false),
'throw' => true,
],
my .env is like this:
AWS_ACCESS_KEY_ID=key
AWS_SECRET_ACCESS_KEY=secret
AWS_DEFAULT_REGION=sa-east-1
AWS_BUCKET=bucket_name
AWS_USE_PATH_STYLE_ENDPOINT=true
the controller method for storing the image is like this:
public function upload(Request $request)
{
$request->validate([
'image' => 'required|image|mimes:jpeg,png,jpg,webp|max:10240',
]);
$uploadedPath = Storage::disk('s3')->put('', $request->file('image'));
dd($uploadedPath);
}
i will share my bucket config as well:
policy:
{
"Version": "2012-10-17",
"Id": "Policy1692807538499",
"Statement": [
{
"Sid": "Stmt1692807537432",
"Effect": "Allow",
"Principal": "*",
"Action": "s3:*",
"Resource": "arn:aws:s3:::muitocarrobkt"
}
]
}
CORS:
[
{
"AllowedHeaders": [],
"AllowedMethods": [
"GET",
"PUT",
"POST",
"DELETE"
],
"AllowedOrigins": [
"*"
],
"ExposeHeaders": []
}
]
I'm completely lost here, I've read related questions on several forums, but none of them solved my problem. Any help would be greatly appreciated.
Thank you very much in advance
I tried to change the way the image is uploaded, using different commands, I redid all the steps in the tutorial, I tried different tutorials, but the error remained the same.
As incredible as it may seem, after hours of trying, I solved my problem. And it was completely local.
In the 's3' configuration file I added the following line
'scheme' => env('AWS_SCHEME', 'https')
in the .env I added the line:
AWS_SCHEME=http
I already suspected that it was something related to my system running locally, while not in production.