illusionist/flysystem-aliyun-oss

阿里云OSS SDK的Flysystem适配器

1.0.1 2022-02-24 06:29 UTC

This package is auto-updated.

Last update: 2024-09-24 12:04:51 UTC


README

阿里云OSS SDK的Flysystem适配器

packagist php downloads license Build Status



英文 | 中文

优点

  1. 支持Laravel & Lumen
  2. xxtime/flysystem-aliyun-oss相比,更符合flysystem接口规范。因为flysystem接口建议返回值是数组或bool,但xxtime/flysystem-aliyun-oss对异常处理不是很严格。
  3. apollopy/flysystem-aliyun-oss <= 1.2.0相比,支持可见性get/set。
  4. 支持动态调用OSS SDK方法。

ps: 对类似项目的比较只是为了突出差异。实际上,它们都非常优秀。

安装

通过composer安装

运行以下命令以拉取最新版本

composer require illusionist/flysystem-aliyun-oss

Laravel安装

如果你的laravel版本 <=5.4,将服务提供者添加到config/app.php配置文件的providers数组中,如下所示

'providers' => [

    ...

    Illusionist\Flysystem\Aliyun\OssServiceProvider::class,
]
Lumen安装

将以下代码片段添加到bootstrap/app.php文件中的提供者部分,如下所示

...

// Add this line
$app->register(Illusionist\Flysystem\Aliyun\OssServiceProvider::class);
Laravel/Lumen配置

将适配器配置添加到config/filesystems.php配置文件的disks数组中,如下所示

'disks' => [
    ...

    'aliyun-oss' => [

        'driver' => 'aliyun-oss',

        /**
         * The AccessKeyId from OSS or STS.
         */
        'key' => '<your AccessKeyId>',

        /**
         * The AccessKeySecret from OSS or STS
         */
        'secret' => '<your AccessKeySecret>',

        /**
         * The domain name of the datacenter.
         *
         * @example: oss-cn-hangzhou.aliyuncs.com
         */
        'endpoint' => '<endpoint address>',

        /**
         * The bucket name for the OSS.
         */
        'bucket' => '<bucket name>',

        /**
         * The security token from STS.
         */
        'token' => null,

        /**
         * If this is the CName and binded in the bucket.
         *
         * Values: true or false
         */
        'cname' => false,
        
        /**
         * Path prefix
         */
        'prefix' => '',
        
        /**
         *  Request header options.
         * 
         *  @example [x-oss-server-side-encryption => 'KMS']
         */
        'options' => []
    ]
]

使用方法

基本

请参阅filesystem-api

use Illusionist\Flysystem\Aliyun\OssAdapter;
use League\Flysystem\Filesystem;
use OSS\OssClient;

$client = new OssClient(
    '<your AccessKeyId>',
    '<your AccessKeySecret>',
    '<endpoint address>'
);

$adapter = new OssAdapter($client, '<bucket name>', 'optional-prefix', 'optional-options');
$filesystem = new Filesystem($adapter);

$filesystem->has('file.txt');

// Dynamic call SDK method.
$adapter->setTimeout(30);
$filesystem->getAdapter()->setTimeout(30);
Laravel/Lumen

请参阅filesystem

use Illuminate\Support\Facades\Storage;

Storage::disk('aliyun-oss')->get('path');

// Dynamic call SDK method.
Storage::disk('aliyun-oss')->getAdapter()->setTimeout(30);