在 Laravel 5 中集成七牛云存储实现云存储功能

WBOY
Release: 2016-06-23 13:10:34
Original
1558 people have browsed it

本 扩展包 基于 https://github.com/qiniu/php-sdk 开发,是七牛云储存Laravel 5Storage版,通过本扩展包可以在Laravel 5中集成七牛云存储功能。

1、安装配置

使用之前,先通过Composer安装:

composer require zgldh/qiniu-laravel-storage
Copy after login

然后在 config/app.php 中注册服务提供者:

zgldh\QiniuStorage\QiniuFilesystemServiceProvider::class
Copy after login

接下来在 config/filesystems.php 里的 disks 中新增如下选项:

'disks' => [        ... ,        'qiniu' => [            'driver'  => 'qiniu',            'domains' => [                'default'   => 'laravelacademy.com1.z0.glb.clouddn.com', //你的七牛域名                'https'     => 'dn-laravelacademy.qbox.me',         //你的HTTPS域名                'custom'    => 'static.laravelacademy.org',     //你的自定义域名             ],            'access_key'=> '',  //AccessKey            'secret_key'=> '',  //SecretKey            'bucket'    => '',  //Bucket名字            'notify_url'=> '',  //持久化处理回调地址        ],    ],
Copy after login

2、使用

第一种用法:

$disk = \Storage::disk('qiniu');$disk->exists('file.jpg');                      //文件是否存在$disk->get('file.jpg');                         //获取文件内容$disk->put('file.jpg',$contents);               //上传文件$disk->prepend('file.log', 'Prepended Text');   //附加内容到文件开头$disk->append('file.log', 'Appended Text');     //附加内容到文件结尾$disk->delete('file.jpg');                      //删除文件$disk->delete(['file1.jpg', 'file2.jpg']);$disk->copy('old/file1.jpg', 'new/file1.jpg');  //复制文件到新的路径$disk->move('old/file1.jpg', 'new/file1.jpg');  //移动文件到新的路径$size = $disk->size('file1.jpg');               //取得文件大小$time = $disk->lastModified('file1.jpg');       //取得最近修改时间 (UNIX)$files = $disk->files($directory);              //取得目录下所有文件$files = $disk->allFiles($directory);               //这个没实现。。。$directories = $disk->directories($directory);      //这个也没实现。。。$directories = $disk->allDirectories($directory);   //这个也没实现。。。$disk->makeDirectory($directory);               //这个其实没有任何作用$disk->deleteDirectory($directory);             //删除目录,包括目录下所有子文件子目录$disk->getDriver()->uploadToken('file.jpg');                //获取上传Token$disk->getDriver()->downloadUrl('file.jpg');                //获取下载地址$disk->getDriver()->downloadUrl('file.jpg', 'https');       //获取HTTPS下载地址$disk->getDriver()->privateDownloadUrl('file.jpg');         //获取私有bucket下载地址$disk->getDriver()->privateDownloadUrl('file.jpg', 'https');//获取私有bucket的HTTPS下载地址$disk->getDriver()->imageInfo('file.jpg');                  //获取图片信息$disk->getDriver()->imageExif('file.jpg');                  //获取图片EXIF信息$disk->getDriver()->imagePreviewUrl('file.jpg','imageView2/0/w/100/h/200');                         //获取图片预览URL$disk->getDriver()->persistentFop('file.flv','avthumb/m3u8/segtime/40/vcodec/libx264/s/320x240');   //执行持久化数据处理$disk->getDriver()->persistentFop('file.flv','fop','队列名');   //使用私有队列执行持久化数据处理$disk->getDriver()->persistentStatus($persistent_fop_id);       //查看持久化数据处理的状态。
Copy after login

第二种用法 (就是省略了一个getDriver):

use zgldh\QiniuStorage\QiniuStorage;$disk = QiniuStorage::disk('qiniu');$disk->exists('file.jpg');                      //文件是否存在$disk->get('file.jpg');                         //获取文件内容$disk->put('file.jpg',$contents);               //上传文件$disk->prepend('file.log', 'Prepended Text');   //附加内容到文件开头$disk->append('file.log', 'Appended Text');     //附加内容到文件结尾$disk->delete('file.jpg');                      //删除文件$disk->delete(['file1.jpg', 'file2.jpg']);$disk->copy('old/file1.jpg', 'new/file1.jpg');  //复制文件到新的路径$disk->move('old/file1.jpg', 'new/file1.jpg');  //移动文件到新的路径$size = $disk->size('file1.jpg');               //取得文件大小$time = $disk->lastModified('file1.jpg');       //取得最近修改时间 (UNIX)$files = $disk->files($directory);              //取得目录下所有文件$files = $disk->allFiles($directory);               //这个没实现。。。$directories = $disk->directories($directory);      //这个也没实现。。。$directories = $disk->allDirectories($directory);   //这个也没实现。。。$disk->makeDirectory($directory);               //这个其实没有任何作用$disk->deleteDirectory($directory);             //删除目录,包括目录下所有子文件子目录$disk->uploadToken('file.jpg');                     //获取上传Token$disk->downloadUrl('file.jpg');                     //获取下载地址$disk->downloadUrl('file.jpg', 'https');            //获取HTTPS下载地址$disk->privateDownloadUrl('file.jpg');              //获取私有bucket下载地址$disk->privateDownloadUrl('file.jpg', 'https');     //获取私有bucket的HTTPS下载地址$disk->imageInfo('file.jpg');                       //获取图片信息$disk->imageExif('file.jpg');                       //获取图片EXIF信息$disk->imagePreviewUrl('file.jpg','imageView2/0/w/100/h/200');                          //获取图片预览URL$disk->persistentFop('file.flv','avthumb/m3u8/segtime/40/vcodec/libx264/s/320x240');    //执行持久化数据处理$disk->persistentFop('file.flv','fop','队列名');    //使用私有队列执行持久化数据处理$disk->persistentStatus($persistent_fop_id);        //查看持久化数据处理的状态。
Copy after login

3、相关参考

本扩展包GitHub地址为 https://github.com/zgldh/qiniu-laravel-storage ,基于 https://github.com/qiniu/php-sdk 开发,更多详情请参考七牛官方PHP SDK使用指南: http://developer.qiniu.com/code/v7/sdk/php.html

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template