luckydate2021/flysystem-us3

Flysystem适配Ucloud US3存储。

v1.0.8 2022-11-26 12:00 UTC

This package is auto-updated.

Last update: 2024-09-26 16:46:17 UTC


README

Flysystem适配Ucloud US3存储。

使用

  1. 拉取

    composer require luckydate2021/flysystem-us3

    如果提示找不到,请还原composer的默认仓库再试。注意,此操作仅限hyperf(^2.2)使用。

    如果提示league/flysystem存在版本问题,请先指定league/flysystem为^1.0再拉取

    composer require "league/flysystem:^1.0"

  2. 按照hyperf的手册加入filesystem,并进行发布

    composer require hyperf/filesystem

    发布

    php bin/hyperf.php vendor:publish hyperf/filesystem

  3. 发布后,在config下autoload有个file.php,修改并增加

    'us3' => [
    'driver' => \Luckydate2021\Flysystem\Us3\Us3AdapterFactory::class, 
    'public_key' => env('US3_PUBLIC_KEY'),
    'secret_key' => env('US3_SECRET_KEY'),
    'bucket' => env('US3_BUCKET'),
    'suffix' => env('US3_SUFFIX'), //eg. ".ufile.ucloud.cn"
    'path_prefix' => '',
    'https' => false,
    ]
    
    
  4. 由于第三方flysystem返回writeStream的结果是布尔值,但ucloud错误是json,导致错误也是true;可以创建一个目录例如patch,然后把Filesystem.php复制到目录,删除强制转换的(bool),直接返回ucloud结果。然后在composer.json中,加入这个文件的自动加载。

    备注:ucloud上传成功返回的文件名,错误时返回的是一个json,其中包含错误信息。

"autoload": {
        "psr-4": {
            "App\\": "app/"
        },
        "files": [
            "patch/Filesystem.php"
        ]
    },

示例

use Hyperf\Filesystem\FilesystemFactory;
use Hyperf\Di\Annotation\Inject;

----- class -----

/**
* @Inject()
* @var FilesystemFactory
*/
protected $filesystemFactory;

public function index()
    {
        $file = $this->request->file('avatar');
        if ($file->isValid()) {

            $ext = strtolower($file->getExtension());

            $filePathUrl = rand(1000, 9999) . '.' . $ext;

            try {
                $stream = fopen($file->getRealPath(), 'r+');
                $bucket = $this->filesystemFactory->get('us3');
                $bucket->writeStream($filePathUrl, $stream, ['mime' => 'image/png']); //主要这里的mine要根据文件来传,这里只是示例
                if (is_resource($stream)) {
                    fclose($stream);
                }
               //'注意:如果发现无法上传,则检查key之类的是否正确,还是不知道就改一下拉下来的Us3Adapter.php,把writeStream返回的结果打印下,看ucloud的报错信息';
            } catch (\Exception $e) {
                return '上传失败';
            }

        }  

其他注意事项

仅适用于league/flysystem ^1.1版本。

在Us3Sdk中使用了new CoroutineHandler(),仅限hyperf(^2.2)使用。

use Hyperf\Guzzle\CoroutineHandler;

$stack->setHandler(new CoroutineHandler());

一定要传递正确的文件类型mime,否则Ucloud默认的是二进制文件 application/octet-stream,容易导致格式错误。

例如 $bucket->writeStream($filePathUrl, $stream, ['mime' => 'image/png']);