The file system component integrates the famous `League\Flysystem` in the PHP ecosystem (this is also the underlying library of many well-known frameworks such as Laravel). Through reasonable abstraction, the program does not have to perceive whether the storage engine is a local hard disk or a cloud server, realizing decoupling. This component provides coroutine support for common cloud storage services.
## Install
```shell
composer require hyperf/filesystem
```
The versions of `League\Flysystem` components `v1.0`, `v2.0` and `v3.0` have changed greatly, so you need to install the corresponding adapters according to different versions
The `config/autoload/file.php` file will be generated. Set the default driver in this file, and configure the access key, access secret and other information of the corresponding driver, and you can use it.
## use
It can be used by injecting `League\Flysystem\Filesystem` through DI.
The API is as follows:
> The following example is Flysystem v1.0 version, please refer to the official documentation for v2.0 version
```php
<?php
declare(strict_types=1);
namespace App\Controller;
class IndexController extends AbstractController
{
public function example(\League\Flysystem\Filesystem $filesystem)
At some point, you will need to use multiple storage media at the same time. At this point, you can inject `Hyperf\Filesystem\FilesystemFactory` to dynamically choose which driver to use.
```php
<?php
declare(strict_types=1);
namespace App\Controller;
class IndexController
{
public function example(\Hyperf\Filesystem\FilesystemFactory $factory)
{
$local = $factory->get('local');
// Write Files
$local->write('path/to/file.txt', 'contents');
$s3 = $factory->get('s3');
$s3->write('path/to/file.txt', 'contents');
}
}
```
### Configure static resources
If you want to access files uploaded locally via http, please add the following configuration to the `config/autoload/server.php` configuration.
```
return [
'settings' => [
...
// Replace public with the upload directory
'document_root' => BASE_PATH . '/public',
'enable_static_handler' => true,
],
];
```
## Precautions
1. Please make sure to install the `hyperf/guzzle` component for S3 storage to provide coroutine support. For Alibaba Cloud, Qiniu Cloud, and Tencent Cloud, please [Open Curl Hook](/zh-cn/coroutine?id=swoole-runtime-hook-level) to use coroutines. Due to the parameter support of Curl Hook, please use Swoole 4.4.13 or later.
2. Private object storage solutions such as minIO and ceph radosgw all support the S3 protocol and can use the S3 adapter.
3. When using the Local driver, the root directory is the configured address, not the root directory of the operating system. For example, if the local driver `root` is set to `/var/www`, then `/var/www/public/file.txt` on the local disk should be accessed through the flysystem API using `/public/file.txt` or ` public/file.txt` .
4. Taking Alibaba Cloud OSS as an example, the read operation performance comparison of 1 core and 1 process:
```bash
ab -k -c 10 -n 1000 http://127.0.0.1:9501/
```
CURL HOOK is not enabled:
```
Concurrency Level: 10
Time taken for tests: 202.902 seconds
Complete requests: 1000
Failed requests: 0
Keep-Alive requests: 1000
Total transferred: 146000 bytes
HTML transferred: 5000 bytes
Requests per second: 4.93 [#/sec] (mean)
Time per request: 2029.016 [ms] (mean)
Time per request: 202.902 [ms] (mean, across all concurrent requests)
Transfer rate: 0.70 [Kbytes/sec] received
```
After enabling CURL HOOK:
```
Concurrency Level: 10
Time taken for tests: 9.252 seconds
Complete requests: 1000
Failed requests: 0
Keep-Alive requests: 1000
Total transferred: 146000 bytes
HTML transferred: 5000 bytes
Requests per second: 108.09 [#/sec] (mean)
Time per request: 92.515 [ms] (mean)
Time per request: 9.252 [ms] (mean, across all concurrent requests)
Transfer rate: 15.41 [Kbytes/sec] received
```
## Detailed configuration
```php
return [
// Select the key corresponding to the driver under storage.