├── .editorconfig
├── README.md
├── composer.json
└── src
└── OssStorageServiceProvider.php
/.editorconfig:
--------------------------------------------------------------------------------
1 | root = true
2 |
3 | [*]
4 | indent_style = space
5 | indent_size = 4
6 | end_of_line = lf
7 | charset = utf-8
8 | trim_trailing_whitespace = true
9 | insert_final_newline = false
10 |
11 | [*.{vue,js,scss}]
12 | charset = utf-8
13 | indent_style = space
14 | indent_size = 2
15 | end_of_line = lf
16 | insert_final_newline = true
17 | trim_trailing_whitespace = true
18 |
19 | [*.md]
20 | trim_trailing_whitespace = false
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
laravel filesystem oss
2 |
3 |
4 | AliOss storage for Laravel based on iidestiny/flysystem-oss.
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 | 感谢关注「GitHub 热门」公众号,带你了解技术圈内热门新鲜事!
19 |
20 |
21 |
22 |
23 | ## 目录
24 | - laravel >= 9 `composer require "iidestiny/laravel-filesystem-oss:^3.1"`
25 | - laravel < 9 `composer require "iidestiny/laravel-filesystem-oss:^2"`
26 |
27 | ## 扩展包要求
28 |
29 | - PHP >= 8.02
30 | - Laravel >= 9
31 |
32 | ## 安装命令
33 |
34 | ```shell
35 | $ composer require "iidestiny/laravel-filesystem-oss:^3.1" -vvv
36 | ```
37 |
38 | ## 配置
39 |
40 | 1. 将服务提供者 `Iidestiny\LaravelFilesystemOss\OssStorageServiceProvider::class` 注册到 `config/app.php` 文件:
41 |
42 | ```php
43 | 'providers' => [
44 | // Other service providers...
45 | Iidestiny\LaravelFilesystemOss\OssStorageServiceProvider::class,
46 | ],
47 | ```
48 |
49 | > Laravel 5.5+ 会自动注册服务提供者可过滤
50 |
51 | 2. 在 `config/filesystems.php` 配置文件中添加你的新驱动
52 |
53 | ```php
54 | [
60 | //...
61 | 'oss' => [
62 | 'driver' => 'oss',
63 | 'root' => '', // 设置上传时根前缀
64 | 'access_key' => env('OSS_ACCESS_KEY'),
65 | 'secret_key' => env('OSS_SECRET_KEY'),
66 | 'endpoint' => env('OSS_ENDPOINT'), // 使用 ssl 这里设置如: https://oss-cn-beijing.aliyuncs.com
67 | 'bucket' => env('OSS_BUCKET'),
68 | 'isCName' => env('OSS_IS_CNAME', false), // 如果 isCname 为 false,endpoint 应配置 oss 提供的域名如:`oss-cn-beijing.aliyuncs.com`,否则为自定义域名,,cname 或 cdn 请自行到阿里 oss 后台配置并绑定 bucket
69 | // 额外自定义初始化 OSS 客户端参数
70 | // 参考:
71 | // 1. https://help.aliyun.com/zh/oss/developer-reference/initialization-6
72 | // 2. \OSS\OssClient::__initNewClient
73 | // 3. \OSS\OssClient::__initClient
74 | 'signatureVersion' => env('OSS_SIGNATURE_VERSION', OssClient::OSS_SIGNATURE_VERSION_V4),
75 | 'region' => env('OSS_REGION', 'cn-hangzhou'),
76 | // 如果有更多的 bucket 需要切换,就添加所有bucket,默认的 bucket 填写到上面,不要加到 buckets 中
77 | 'buckets'=>[
78 | 'test'=>[
79 | 'access_key' => env('OSS_ACCESS_KEY'),
80 | 'secret_key' => env('OSS_SECRET_KEY'),
81 | 'bucket' => env('OSS_TEST_BUCKET'),
82 | 'endpoint' => env('OSS_TEST_ENDPOINT'),
83 | 'isCName' => env('OSS_TEST_IS_CNAME', false),
84 | ],
85 | //...
86 | ],
87 | ],
88 | //...
89 | ]
90 | ];
91 | ```
92 |
93 | ## 基本使用
94 |
95 | ```php
96 | put('avatars/filename.jpg', $fileContents);
102 | ```
103 |
104 | 以上方法可在 [laravel-filesystem-doc](https://laravel.com/docs/5.5/filesystem) 查阅
105 |
106 | ## 进阶使用
107 |
108 | ```php
109 | // 获取文件访问地址「公共读的 bucket 才生效」
110 | $url = $disk->getAdapter()->getUrl('folder/my_file.txt');
111 |
112 | // 设置文件访问有效期「$timeout 为多少秒过期」「私有 bucket 才可看见效果」
113 | $url = $disk->getAdapter()->getTemporaryUrl('cat.png', $timeout, ['x-oss-process' => 'image/circle,r_100']);
114 |
115 | // 可切换其他 bucket「需要在 config 配置文件中配置 buckets」
116 | $exists = $disk->getAdapter()->bucket('test')->xxx('file.jpg');
117 | ```
118 |
119 | ## 获取官方完整 OSS 处理能力
120 |
121 | 阿里官方 SDK 可能处理了更多的事情,如果你想获取完整的功能可通过此插件获取,
122 | 然后你将拥有完整的 oss 处理能力
123 |
124 | ```php
125 | // 获取完整处理能力
126 | $kernel = $disk->getAdapter()->ossKernel();
127 |
128 | // 例如:防盗链功能
129 | $refererConfig = new RefererConfig();
130 | // 设置允许空Referer。
131 | $refererConfig->setAllowEmptyReferer(true);
132 | // 添加Referer白名单。Referer参数支持通配符星号(*)和问号(?)。
133 | $refererConfig->addReferer("www.aliiyun.com");
134 | $refererConfig->addReferer("www.aliiyuncs.com");
135 |
136 | $kernel->putBucketReferer($bucket, $refererConfig);
137 | ```
138 |
139 | > 更多功能请查看[官方 SDK 手册](https://help.aliyun.com/document_detail/32100.html?spm=a2c4g.11186623.6.1055.66b64a49hkcTHv)
140 |
141 | ## 前端 web 直传配置
142 |
143 | oss 直传有三种方式,当前扩展包使用的是最完整的 [服务端签名直传并设置上传回调](https://help.aliyun.com/document_detail/31927.html?spm=a2c4g.11186623.2.10.5602668eApjlz3#concept-qp2-g4y-5db) 方式,扩展包只生成前端页面上传所需的签名参数,前端上传实现可参考 [官方文档中的实例](https://help.aliyun.com/document_detail/31927.html?spm=a2c4g.11186623.2.10.5602668eApjlz3#concept-qp2-g4y-5db) 或自行搜索
144 |
145 | ```php
146 | /**
147 | * 1. 前缀如:'images/'
148 | * 2. 回调服务器 url
149 | * 3. 回调自定义参数,oss 回传应用服务器时会带上
150 | * 4. 当前直传配置链接有效期
151 | */
152 | $config = $disk->getAdapter()->signatureConfig($prefix = '/', $callBackUrl = '', $customData = [], $expire = 30);
153 | ```
154 |
155 | ## 直传回调验签
156 |
157 | 当设置了直传回调后,可以通过验签插件,验证并获取 oss 传回的数据 [文档](https://help.aliyun.com/document_detail/91771.html?spm=a2c4g.11186623.2.15.7ee07eaeexR7Y1#title-9t0-sge-pfr)
158 |
159 | 注意事项:
160 | - 如果没有 Authorization 头信息导致验签失败需要先在 apache 或者 nginx 中设置 rewrite
161 | - 以 apache 为例,修改 httpd.conf 在 DirectoryIndex index.php 这行下面增加「RewriteEngine On」「RewriteRule .* - [env=HTTP_AUTHORIZATION:%{HTTP:Authorization},last]」
162 |
163 | ```php
164 | // 验签,就是如此简单
165 | // $verify 验签结果,$data 回调数据
166 | list($verify, $data) = $disk->getAdapter()->verify();
167 | // [$verify, $data] = $disk->getAdapter()->verify(); // php 7.1 +
168 |
169 | if (!$verify) {
170 | // 验证失败处理,此时 $data 为验签失败提示信息
171 | }
172 |
173 | // 注意一定要返回 json 格式的字符串,因为 oss 服务器只接收 json 格式,否则给前端报 CallbackFailed
174 | return response()->json($data);
175 | ```
176 |
177 | 直传回调验签后返回给前端的数据「包括自定义参数」,例如
178 |
179 | ```json
180 | {
181 | "filename": "user/15854050909488182.png",
182 | "size": "56039",
183 | "mimeType": "image/png",
184 | "height": "473",
185 | "width": "470",
186 | "custom_name": "zhangsan",
187 | "custom_age": "24"
188 | }
189 | ```
190 |
191 | > 这其实要看你回调通知方法具体怎么返回,如果直接按照文档给的方法返回是这个样子
192 |
193 | ## 前端直传组件分享「vue + element」
194 |
195 | ```html
196 |
197 |
198 |
209 |
210 |
211 |
212 |
213 |
214 |
215 |
292 |
293 |
318 |
319 | ```
320 |
321 | ## 依赖的核心包
322 |
323 | - [iidestiny/flysystem-oss](https://github.com/iiDestiny/flysystem-oss)
324 |
325 | ## 参考
326 |
327 | - [overtrue/flysystem-qiniu](https://github.com/overtrue/flysystem-qiniu)
328 |
329 | ## License
330 |
331 | [](https://github.com/996icu/996.ICU/blob/master/LICENSE)
332 |
--------------------------------------------------------------------------------
/composer.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "iidestiny/laravel-filesystem-oss",
3 | "description": "Oss storage filesystem for Laravel.",
4 | "license": "MIT",
5 | "authors": [
6 | {
7 | "name": "iidestiny",
8 | "email": "iidestiny@vip.qq.com"
9 | }
10 | ],
11 | "require": {
12 | "php": "^8.0.2",
13 | "iidestiny/flysystem-oss": "^4.5",
14 | "laravel/framework": "^9.0 | ^10.0 | ^11.0 | ^12.0"
15 | },
16 | "autoload": {
17 | "psr-4": {
18 | "Iidestiny\\LaravelFilesystemOss\\": "src"
19 | }
20 | },
21 | "extra": {
22 | "laravel": {
23 | "providers": [
24 | "Iidestiny\\LaravelFilesystemOss\\OssStorageServiceProvider"
25 | ]
26 | }
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/src/OssStorageServiceProvider.php:
--------------------------------------------------------------------------------
1 |
7 | *
8 | * This source file is subject to the MIT license that is bundled
9 | * with this source code in the file LICENSE.
10 | */
11 |
12 | namespace Iidestiny\LaravelFilesystemOss;
13 |
14 | use Iidestiny\Flysystem\Oss\OssAdapter;
15 | use Illuminate\Filesystem\FilesystemAdapter;
16 | use Illuminate\Support\ServiceProvider;
17 | use League\Flysystem\Filesystem;
18 |
19 | /**
20 | * Class OssStorageServiceProvider
21 | *
22 | * @author iidestiny
23 | */
24 | class OssStorageServiceProvider extends ServiceProvider
25 | {
26 | /**
27 | * Bootstrap the application services.
28 | *
29 | * @return void
30 | */
31 | public function boot()
32 | {
33 | app('filesystem')->extend('oss', function ($app, $config) {
34 | $root = $config['root'] ?? null;
35 | $buckets = $config['buckets'] ?? [];
36 | $extra = array_diff_key($config, array_flip(['driver', 'root', 'buckets', 'access_key', 'secret_key',
37 | 'endpoint', 'bucket', 'isCName', 'url']));
38 |
39 | $adapter = new OssAdapter(
40 | $config['access_key'],
41 | $config['secret_key'],
42 | $config['endpoint'],
43 | $config['bucket'],
44 | $config['isCName'],
45 | $root,
46 | $buckets,
47 | ...$extra,
48 | );
49 |
50 | $adapter->setCdnUrl($config['url'] ?? null);
51 |
52 | return new FilesystemAdapter(new Filesystem($adapter), $adapter, $config);
53 | });
54 | }
55 |
56 | /**
57 | * Register the application services.
58 | *
59 | * @return void
60 | */
61 | public function register()
62 | {
63 | //
64 | }
65 | }
66 |
--------------------------------------------------------------------------------