通过存储从上传的文件中获取CDN网址 [英] Get CDN url from uploaded file via Storage
本文介绍了通过存储从上传的文件中获取CDN网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用存储提供程序将文件上传到机架空间,就像这样...
I am using the Storage provider to upload files to rackspace like so...
$logo = $request->file('logo');
$content = fopen($logo->getRealPath(), 'r+');
\Storage::disk('cdn')->put('logo.png', $content);
现在,如何获取上面文件的网址?我一直在寻找API中的方法,这似乎是不可能的.
Now, how can I get the url of the file above? I has been looking for a method in the API and it seems so impossible.
- http://laravel.com/api/5.0/Illuminate/Filesystem/FilesystemAdapter.html
- http://laravel.com/api/5.0/Illuminate/Filesystem/Filesystem.html
- http://laravel.com/api/5.0/Illuminate/Filesystem/FilesystemAdapter.html
- http://laravel.com/api/5.0/Illuminate/Filesystem/Filesystem.html
推荐答案
我通常将磁盘公共URL存储在config/filesystems.php文件中.例如:
I usually store the disk public URL in the config/filesystems.php file. For example:
'google' => [
'driver' => 's3',
'key' => env('STORAGE_GOOGLE_KEY'),
'secret' => env('STORAGE_GOOGLE_SECRET'),
'bucket' => env('STORAGE_GOOGLE_BUCKET'),
'base_url' => 'https://storage.googleapis.com',
'public_url' => env('STORAGE_GOOGLE_PUBLIC_URL'),
],
然后在我的模型中,为包含文件名的字段定义一个变量:
Then in my model, I define a mutator for the field containing the file name:
public function getAvatarAttribute($value)
{
// If avatar isn't a URL, generate it
if (filter_var($value, FILTER_VALIDATE_URL) !== FALSE) {
return $value;
} else {
$disk = config('filesystems.default');
return config('filesystems.disks.'.$disk.'.public_url').'/avatars/'.$value;
}
}
这允许我:
- 要随时更改默认磁盘,并立即将所有引用指向我的新磁盘.
- 根据需要将实际URL存储在数据库中.就我而言,化身可能来自OAuth或实际文件上传,因此我需要兼顾两者.
这篇关于通过存储从上传的文件中获取CDN网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文