兰空图床Lsky Pro增加AWS S3/Minio支持

兰空图床是一款简单而强大的图床程序,但是目前并不支持minio/s3存储策略,作者计划在2.0版本开发此功能。

然而我不想等了,决定略写几行代码,以支持自建minio后端。有需要的看官可以参考。

大约如下几步:

一、撰写AWS S3存储driver,放到extend/strategy/driver/ 目录下:

<?php

namespace strategy\driver;

use strategy\Driver;

if (!function_exists('exif_imagetype')) {
    function exif_imagetype($filename)
    {
        if ((list($width, $height, $type, $attr) = getimagesize($filename)) !== false) {
            return $type;
        }
        return false;
    }
}

/**
 * Aws储存驱动
 *
 * Class Aws
 * @package strategy\driver
 */
class Aws implements Driver
{
    /**
     * 当前储存策略参数
     *
     * @var array
     */
    protected $options = [];

    /**
     * 错误信息
     *
     * @var null
     */
    protected $error = null;

    /**
     * Aws实例
     *
     * @var null
     */
    protected $s3 = null;

    /**
     * Aws constructor.
     *
     * @param array $options
     */
    public function __construct($options = [])
    {
        $this->options = $options;
        try {
            $this->s3 = new \Aws\S3\S3Client([
                'version' => 'latest',
                'region'  => empty($this->options['s3_region']) ? 'us-east-1' : $this->options['s3_region'],
                'endpoint' => $this->options['s3_endpoint'],
                'use_path_style_endpoint' => true,
                'credentials' => [
                    'key'    => $this->options['s3_key'],
                    'secret' => $this->options['s3_secret'],
                ],
            ]);
        } catch (\Exception $e) {
            $this->error = $e->getMessage();
        }
    }

    /**
     * 创建文件
     *
     * @param $pathname
     * @param $file
     *
     * @return bool
     */
    public function create($pathname, $file)
    {
        try {
            $params = array(
                'Bucket' => $this->options['s3_bucket'],
                'Key' => $pathname,
                'Body' => fopen($file, 'rb')
            );
            if ($image_type = exif_imagetype($file)) {
                $params['ContentType'] = image_type_to_mime_type($image_type);
            }

            $this->s3->putObject($params);
        } catch (\Exception $e) {
            $this->error = $e->getMessage();
            return false;
        }

        return true;
    }

    /**
     * 删除文件
     *
     * @param $pathname
     *
     * @return bool
     */
    public function delete($pathname)
    {
        try {
            $this->s3->deleteObject([
                'Bucket' => $this->options['s3_bucket'],
                'Key' => $pathname,
            ]);
        } catch (\Exception $e) {
            $this->error = $e->getMessage();
            return false;
        }

        return true;
    }

    /**
     * 删除多个文件
     *
     * @param array $list
     * @return bool|mixed
     */
    public function deletes(array $list)
    {
        try {
            $objects = [];
            foreach ($list as $value) {
                $objects[] = ['Key' => $value ];
            }
            $this->s3->deleteObjects([
                'Bucket' => $this->options['s3_bucket'],
                'Objects' => $objects,
            ]);
        } catch (\Exception $e) {
            $this->error = $e->getMessage();
            return false;
        }

        return true;
    }

    public function getError()
    {
        return 'Aws:' . $this->error;
    }
}

二、增加aws-sdk-php依赖

composer require aws/aws-sdk-php -n

三、增加存储策略配置,在config/strategy.php 增加一项

'aws'=>['name'=>'AWS S3','class'=>\strategy\driver\Aws::class],

四、执行SQL增加配置参数

INSERT INTO `lsky_config` VALUES (0,'aws','text','text','s3_endpoint','Endpoint',NULL,'',''),(0,'aws','text','text','s3_key','Key',NULL,'',''),(0,'aws','text','text','s3_secret','Secret',NULL,'',''),(0,'aws','text','text','s3_bucket','Bucket','储存桶名称','',''),(0,'aws','text','text','aws_cdn_domain','域名',NULL,'','');

五,在后台设置存储策略,完成

 

附:构建docker参考脚本 docker.zip