I managed to solve it by remove the if part and simplified it to
Storage::disk('s3')->put($destination_path.$filename, file_get_contents($files), 'public');
I uploaded successfully files into amazon s3, but funnily the size of file is not as expected. I have file size around 1MB, but after uploaded I saw the file around 50-ish bytes. What did I do wrong? Here is the code I have done so far.
routes.php
<?php
use Illuminate\Support\Facades\Storage;
use App\Image;
use Illuminate\Http\Request;
use App\Http\Requests;
Route::get('/', function () {
return view('welcome');
});
Route::post('upload', function(Request $request){
$image = new Image;
$image->id = $request->input('id');
$files = $request->file('file');
$destination_path = 'uploads/';
$fileName = time().'-'.$files->getClientOriginalName();
$fileSize = $request->file('file')->getClientSize();
$files->move($destination_path,$fileName);
$image->file = $destination_path . $fileName;
$image->save();
//Store into amazon aws
$s3 = Storage::disk('s3');
//if($s3->put($fileName,file_get_contents($request->file('file')),'public')){
if($s3->put($fileName,$image->file,'public')){
$file = Image::create([
'fileName' => $fileName,
'fileSize' => $fileSize,
'filePath' => env('S3_URL').$fileName,
'type' => 's3',
]);
}
return "Done";
});
If I use if($s3->put($fileName,file_get_contents($request->file('file')),'public')){ then I will get ErrorException in routes.php line 39: file_get_contents(/tmp/php8Mj2hK): failed to open stream: No such file or directory It seems obvious that when I uses $image->file in the second parameter of put function will give the size of the link instead of size of the actual file. I still dont know how to get the actual file from the upload/ folder and send it to amazon s3. How to do it correctly? Thanks
I managed to solve it by remove the if part and simplified it to
Storage::disk('s3')->put($destination_path.$filename, file_get_contents($files), 'public');
Please or to participate in this conversation.