I am using S3 to upload and download files. The files needs to be private; they cant be public.
The below code actually works, but I was wondering whats the correct/better way to do the download?
# the upload codes looks something like this....
$file = request()->file('file_name');
$file_name = \Str::random(9) . $file->getClientOriginalName() );
$path = 'secure/files/' ;
\Storage::disk('s3')->put( $path.$file_name , file_get_contents($file) ); //, 'public'
//Save file in in the DB
and the download codes looks like this
<?php
namespace App\Helpers;
class S3
{
public function download()
{
request()->validate([
'filename' =>'required',
'path' =>'required',
]);
$path = request()->path;
$filename = request()->filename;
$file = \Storage::disk('s3')->get($path.$filename);
$headers = [
'Content-Type' => 'text/csv',
'Content-Description' => 'File Transfer',
'Content-Disposition' => "attachment; filename={$filename}",
'filename'=> '{$filename}'
];
return response( $file, 200, $headers );
}
}
Download route looks like this
Route::get('/download', '\App\Helpers\S3@download')->middleware('auth');
and use it like this
<a href="{{ url("download?filename={$file->name}&path={$file->path}") }}" > Open </a>