【问题标题】:How to return the random S3 filename upon Laravel upload?如何在 Laravel 上传时返回随机 S3 文件名?
【发布时间】:2019-10-26 00:18:04
【问题描述】:

我正在尝试将视频从我的 Laravel 应用程序上传到我的 S3 存储桶。上传工作正常,但现在我想获取文件的 url,并将其存储在数据库记录中。

目前,我可以上传文件,并将我认为来自 S3 的 url 存储在数据库中。这些都不是问题。但是会发生什么是 S3 生成该随机文件名。我对此很好,但我想以某种方式将它返回给控制器,以便我可以将它与数据库中的路径一起存储。

我正在使用:

  • Laravel 5.8.19
  • 一个 S3 存储桶
  • 联赛/flysystem-aws-s3-v3

这是我的控制器:

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();
}

有什么想法吗?

【问题讨论】:

  • 请注意,如果有人不上传video_file,您的代码将失败;您的验证没有将其标记为required,因此在某些情况下$theFile 可能是null,因此$theFile->storePublicly() 会引发错误。另外,您不需要重新查询Game::find($getGameID);;你已经有了$game,你可以直接打电话给$game->gameVid = $url; $game->save();
  • 感谢您的验证。我试图让上传正常工作,所以我匆匆完成了一点,但现在是时候添加它了。另外,感谢关​​于 $game 的建议。我不确定这是否会奏效,但我很高兴知道它会奏效。
  • 没问题 :) 我知道这些建议对实际答案没有影响,这就是我将它们作为评论留下的原因。是的,在您的代码中,$game$gameVid 在数据库中是相同的记录。您的操作方式只是稍微不那么理想,所以不是一个大问题,但无论如何都要注意。

标签: php laravel amazon-web-services amazon-s3 file-upload


【解决方案1】:

从 S3 上传和检索文件的最简单方法是使用 Storage 门面。

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

To upload the file 到 Amazon S3。该文件以您提供的名称存储,因此您有一个可预测的文件名。例如,您可以将文件名保存在数据库中,以便以后检索它。

然后您可以稍后检索保存的文件:

Storage::disk('s3')->get('file.txt');

【讨论】:

  • 我试过了,它仍然在创建一个以 .mp4 结尾的文件夹。文件夹内是我随机命名的文件,扩展名为 .qt???知道为什么吗? @namelivia
【解决方案2】:

我在发表之前看过这篇文章,但我误解了我的问题,认为他的问题与此无关。原来这个问题的答案在这里找到:Laravel S3 image upload creates a folder with the filename automatically

【讨论】:

    【解决方案3】:
    $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');
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-13
      • 2013-10-17
      • 2017-08-24
      相关资源
      最近更新 更多