0

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?

Lakhwinder Singh
  • 5,536
  • 5
  • 27
  • 52
Hunter
  • 438
  • 1
  • 5
  • 16
  • Just a note, you code will fail if someone doesn't upload a `video_file`; your validation doesn't have it marked as `required`, so there are instances where `$theFile` could be `null`, so `$theFile->storePublicly()` would throw an error. Also, you don't need to re-query `Game::find($getGameID);`; you already have `$game`, which you can just call `$game->gameVid = $url; $game->save();` – Tim Lewis Jun 11 '19 at 14:47
  • Thank you on the validation thing. I was trying to get the upload just working so I rushed through it a bit, but it is time to add that in. Also, thanks for the advice on $game. I wasn't sure if that would work but I am glad to know it will. – Hunter Jun 11 '19 at 18:53
  • No problem :) I know those suggestions had no impact on the actual answer, which is why I left them as a comment. And yeah, in your code `$game` and `$gameVid` are the same record in the database. It's only slightly less optimal the way you're doing it, so not a huge issue, but good to be aware of regardless. – Tim Lewis Jun 11 '19 at 18:59

3 Answers3

1

I had looked at this post before I made mine, but I misunderstood my problem, thinking that his one was unrelated. Turns out the answer to this question is found here: Laravel S3 image upload creates a folder with the filename automatically

Hunter
  • 438
  • 1
  • 5
  • 16
0

The simplest way to upload and retrieve files from S3 is using the Storage facade.

Storage::disk('s3')->put('file.txt', $fileContent);

To upload the file to Amazon S3. The file is stored with the name you provided, so you have a predictable filename. You can save the filename for example in the database so you can later retrieve it.

Then you can later retrieve the saved file with:

Storage::disk('s3')->get('file.txt');
namelivia
  • 2,657
  • 1
  • 21
  • 24
  • 2
    I tried this, and it still is making a folder that just ends in .mp4. Inside the folder is my randomly-named file with the extension .qt??? Any idea why that is? @namelivia – Hunter Jun 11 '19 at 19:00
0
$yourFile = $request->file('<your request name for file>');
$extension = $yourFile->getClientOriginalExtension();
$newName = <new name> . $extension;

Storage::disk('s3')->put("<make a path if you want in to be saved in a folder>".$newName , file_get_contents($yourFile ), 'public');