将S3 URL存储在数据库表中(Store S3 URL in database table)

编程入门 行业动态 更新时间:2024-10-27 23:29:12
将S3 URL存储在数据库表中(Store S3 URL in database table)

我正在Laravel 5.2中构建一个小型资产管理系统

用户可以将图像,视频等上传到应用程序,并且资产元数据存储在资产表中。 当发生这种情况时,资产被重命名以匹配资产ID(我也存储原始文件名),我正在存储mime类型并将文件上传到S3。

我来的地方是将S3 url存储在数据库中。

这是我的方法

public function store(AssetRequest $request) { // Initialise new asset and set the name // from the form $asset = new Asset(array( 'name' => $request->get('name') )); $asset->user_id = Auth::user()->id; // save the asset to the db $asset->save(); // set the file var = form input $file = $request->file('asset_path'); $extension = $file->getClientOriginalExtension(); // modify the asset name $assetFile = $asset->id . '.' . $request->file('asset_path')->getClientOriginalExtension(); // push the new asset to s3 Storage::disk('s3')->put('uploads/' . $assetFile, file_get_contents($file)); $asset->mime = $file->getClientMimeType(); $s3Url = Storage::url($file); $asset->s3_url = $s3Url; $asset->original_filename = $file->getClientOriginalName(); $asset->filename = $assetFile; $asset->file_extension = $extension; // return ok $asset->save(); return \Redirect::route('asset.create')->with('message', 'Asset added!'); }

与我尝试存储S3网址有关的行

$s3Url = Storage::url($file); $asset->s3_url = $s3Url; 似乎只存储临时路径/storage//tmp/php8su2r0而不是实际的S3网址。 我想避免手动设置存储桶,而是希望我可以使用config/filesystem.php中config/filesystem.php

有任何想法吗?

I'm building a small asset management system in Laravel 5.2

A user can upload images, video etc to the app and the asset meta data gets stored in the assets table. While that's happening, the asset is renamed to match the asset id (I'm storing the original filename too), I'm storing the mime type and uploading the file to S3.

Where I've come unstuck is storing the S3 url in database.

This is my method

public function store(AssetRequest $request) { // Initialise new asset and set the name // from the form $asset = new Asset(array( 'name' => $request->get('name') )); $asset->user_id = Auth::user()->id; // save the asset to the db $asset->save(); // set the file var = form input $file = $request->file('asset_path'); $extension = $file->getClientOriginalExtension(); // modify the asset name $assetFile = $asset->id . '.' . $request->file('asset_path')->getClientOriginalExtension(); // push the new asset to s3 Storage::disk('s3')->put('uploads/' . $assetFile, file_get_contents($file)); $asset->mime = $file->getClientMimeType(); $s3Url = Storage::url($file); $asset->s3_url = $s3Url; $asset->original_filename = $file->getClientOriginalName(); $asset->filename = $assetFile; $asset->file_extension = $extension; // return ok $asset->save(); return \Redirect::route('asset.create')->with('message', 'Asset added!'); }

The lines relating to my attempt at storing the S3 url

$s3Url = Storage::url($file); $asset->s3_url = $s3Url; Only seems to store a temporary path /storage//tmp/php8su2r0 rather than an actual S3 url. I'd like to avoid having to set the bucket manually, rather hoping I can use what is configured in config/filesystem.php

Any ideas?

最满意答案

您可以使用config(key)函数帮助程序从配置中获取所有内容,以便获取文件的s3公共URL,执行以下操作:

function publicUrl($filename){ return "http://".config('filesystems.disks.s3.bucket').".s3-website.".config('filesystems.disks.s3.region').".amazonaws.com/".$filename; }

或者你可以使用底层的S3Client :( 取自这里 )

$filesystem->getAdapter()->getClient()->getObjectUrl($bucket, $key);

you can get everything from the config using the config(key) function helper so to get the s3 public url of file, do this:

function publicUrl($filename){ return "http://".config('filesystems.disks.s3.bucket').".s3-website.".config('filesystems.disks.s3.region').".amazonaws.com/".$filename; }

or you can use the underlying S3Client:(taken from here)

$filesystem->getAdapter()->getClient()->getObjectUrl($bucket, $key);

更多推荐

本文发布于:2023-08-05 21:48:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1440261.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据库   URL   table   database   Store

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!