首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在Laravel上传时返回随机的S3文件名?

如何在Laravel上传时返回随机的S3文件名?
EN

Stack Overflow用户
提问于 2019-06-11 22:28:55
回答 3查看 2K关注 0票数 0

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

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

我正在使用:

  • Laravel 5.8.19
  • An S3 Laravel

这里的是我的控制器:

代码语言:javascript
复制
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();
}

有什么想法吗?

EN

回答 3

Stack Overflow用户

发布于 2019-06-12 03:25:23

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

票数 1
EN

Stack Overflow用户

发布于 2019-06-11 22:49:23

从S3上传和检索文件的最简单方法是使用存储外观。

代码语言:javascript
复制
Storage::disk('s3')->put('file.txt', $fileContent);

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

然后,您可以稍后使用以下命令检索已保存的文件:

代码语言:javascript
复制
Storage::disk('s3')->get('file.txt');
票数 0
EN

Stack Overflow用户

发布于 2019-12-13 11:20:20

代码语言:javascript
复制
$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');
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56546015

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档