Larave 结合用阿里云 OSS 作为 Storage 存储文件
Aliyun-oss-storage for Laravel 5+
借鉴了一些优秀的代码,综合各方,同时做了更多优化,将会添加更多完善的接口和插件,打造Laravel最好的OSS Storage扩展
参考
Require
- Laravel 5+
- cURL extension
安装
只需将下面这行
"jacobcyl/ali-oss-storage": "dev-master"
添加到你项目的composer.json require内. 然后运行 composer install
or composer update
.
Or, you can simple run below command
"composer require jacobcyl/ali-oss-storage:dev-master"
来安装
在 config/app.php
文件中添加provider
Jacobcyl\AliOSS\AliOssServiceProvider::class,
配置
配置 app/filesystems.php:
'disks'=>[
...
'oss' => [
'driver' => 'oss',
'access_id' => '<你阿里云 AccessKeyId>',
'access_key' => '<你阿里云 AccessKeySecret>',
'bucket' => '<OSS bucket 名称>',
'endpoint' => '<节点名称或自定义域名>',
'isCName' => <如果上面使用了节点名称,这里设置为false,如果使用了自定义域名,为true>,
'debug' => '<true|false>',
],
...
]
设置默认驱动 app/filesystems.php:
'default' => 'oss',
好了,设置完成后,你就可以像平时使用storage一样使用阿里云OSS了
使用
具体使用也可以参考官方Storage文档 Larave doc for Storage
常用接口如下
首先引入Storage facade
use Storage;
接下来就可以适用所有Storage接口,这里也新增了一些接口
Storage::disk('oss'); // if default filesystems driver is oss, you can skip this step
//fetch all files of specified bucket(see upond configuration)
Storage::files($directory);
Storage::allFiles($directory);
Storage::put('path/to/file/file.jpg', $contents); //first parameter is the target file path, second paramter is file content
Storage::putFile('path/to/file/file.jpg', 'local/path/to/local_file.jpg'); //新增接口,根据本地路径上传本地文件
Storage::get('path/to/file/file.jpg'); // get the file object by path
Storage::exists('path/to/file/file.jpg'); // determine if a given file exists on the storage(OSS)
Storage::size('path/to/file/file.jpg'); // get the file size (Byte)
Storage::lastModified('path/to/file/file.jpg'); // get date of last modification
Storage::directories($directory); // Get all of the directories within a given directory
Storage::allDirectories($directory); // Get all (recursive) of the directories within a given directory
Storage::copy('old/file1.jpg', 'new/file1.jpg');
Storage::move('old/file1.jpg', 'new/file1.jpg');
Storage::rename('path/to/file1.jpg', 'path/to/file2.jpg');
Storage::prepend('file.log', 'Prepended Text'); // Prepend to a file.
Storage::append('file.log', 'Appended Text'); // Append to a file.
Storage::delete('file.jpg');
Storage::delete(['file1.jpg', 'file2.jpg']);
Storage::makeDirectory($directory); // Create a directory.
Storage::deleteDirectory($directory); // Recursively delete a directory.It will delete all files within a given directory, SO Use with caution please.
文档
更详细的OSS文档请移步 Aliyun OSS DOC
如果还有什么功能需要扩展可以联系我,在github提一个issues,欢迎大家一起改进
详情请移步Aliyun-oss-storage
本帖已被设为精华帖!
本帖由系统于 7年前 自动加精
mark
厉害了 可以用!!!!
求教 图片怎么解冻呢
怎么获取它的返回地址呢
@ruan 需要自己拼接
支持服务端签名后直传吗?
请求的结果返回是空. 怎么调试呢
@rayjun 请问怎么生成他有私有带签名的链接地址呢?阿里云的PHP-SDK有他的方法,不知道这个怎么弄。
这是阿里云的SDK封装好的方法:https://help.aliyun.com/document_detail/32...
用laravel的storage好像没有这个方法,自己写的话有什么思路?
手动大神@liyu001989
@liyu001989
@daviLee 为什么不艾特这个包的作者,艾特我啊 :sweat: 。你的意思阿里云的 sdk 中有的功能,这个包没有封装?问问楼主啊,或者去这个包的 issue 中提问
@liyu001989 哈哈已经养成了没事就@你的习惯,嗯嗯我去github提 issue 吧,因为我看这个楼主丢下这篇文章就没出现过了。。。
尝试呼叫楼主@jacobcyl
在 配置 中 写 自定义域名 会
不行啊 签名问题没有解决 可以上传删除 就是不能获取到url
为什么我只会上传0.06kb,无论图片大小都是这样。求解释一下,谢谢