I am trying to upload video from my Laravel application to my S3 bucket. The uploads are working just fine, but now I want to grab the url of the file, and store it in the database record as well.
Currently, I can upload the file, and store what I think is the url from S3 in the database. None of that is a problem. What happens though is that S3 generates that random file name. I am fine with that, but I would like to return it to the controller somehow so that I can store it with the path in the db.
I am using:
- Laravel 5.8.19
- An S3 Bucket
- league/flysystem-aws-s3-v3
Here is my controller:
public function store(Request $request)
{
//Validate Form Data
$this->validate($request, [
'opponent' => 'required',
'location' => 'required',
'date' => 'required',
'team_id' => 'required',
'season_id' => 'required',
'team_score' => 'required',
'opponent_score' => 'required',
'uploading_coach' => 'required',
'periods' => 'required',
'period_length' => 'required',
]);
//Store all the text fields, not the video
$game = new Game;
$game->opponent = $request->input('opponent');
$game->location = $request->input('location');
$game->date = $request->input('date');
$game->team_id = $request->input('team_id');
$game->season_id = $request->input('season_id');
$game->team_score = $request->input('team_score');
$game->opponent_score = $request->input('opponent_score');
$game->uploading_coach = $request->input('uploading_coach');
$game->periods = $request->input('periods');
$game->period_length = $request->input('period_length');
$game->save();
//Set up some variables needed below
$getGameID = $game->id;
$team_id = $game->team_id;
$game_date = $game->date;
//Handles the actual file upload to S3
$theFile = $request->file('video_file');
$name = 'game_date-' . $game_date . 'game_id-' . $getGameID;
$theFile->storePublicly(
'gameid:' . $getGameID . 'teamid:' . $team_id . '/' . $name,
's3'
);
//Game film is now uploaded to S3, trying to get the url and store it in the db
$url = Storage::disk('s3')->url('gameid:' . $getGameID . 'teamid:' . $team_id . "/" . $name);
$gameVid = Game::find($getGameID);
$gameVid->video_link = $url;
$gameVid->save();
return back();
}
Any ideas?