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

[复制链接]
查看: 1849   回复: 9
发表于 2022-2-9 17:33:05 | 显示全部楼层 |阅读模式
兰空图床是一款简单而强大的图床程序,但是目前并不支持minio/s3存储策略,作者计划在2.0版本开发此功能。

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

大约如下几步:

一、撰写AWS S3存储driver,放到extend/strategy/driver/ 目录下:
  1. <?phpnamespace 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{    /**     * 当前储存策略参数     *     * [url=home.php?mod=space&uid=32455]@var[/url] 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依赖
  1. composer require aws/aws-sdk-php -n
复制代码
三、增加存储策略配置,在config/strategy.php 增加一项
  1. 'aws'=>['name'=>'AWS S3','class'=>\strategy\driver\Aws::class],
复制代码
四、执行SQL增加配置参数
  1. 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,'','');
复制代码
五,在后台设置存储策略,完成

原文:https://www.wellphp.com/2022/02/ ... %e6%94%af%e6%8c%81/

觉得有用可以考虑给我打赏 https://shop.cgs.me/buy/1
回复

使用道具 举报

 楼主| 发表于 2022-2-9 17:33:20 | 显示全部楼层
他不是在做2.0吗,等2.0这些都支持了兰空图床Lsky Pro增加AWS S3/Minio支持

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
回复 支持 反对

使用道具 举报

发表于 2022-2-9 17:34:00 | 显示全部楼层
前排支持





asbd zsbd
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-2-9 17:34:49 | 显示全部楼层
本来想夸夸你。你这直接掏二维码……
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-2-9 17:35:03 | 显示全部楼层
海苔 发表于 2022-2-9 12:33
他不是在做2.0吗,等2.0这些都支持了
看第一行和第二行
回复 支持 反对

使用道具 举报

发表于 2022-2-9 17:35:48 | 显示全部楼层
说得好像1.0和2.0不互通。这样的话搭建2.0数据就没了。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-2-9 17:36:45 | 显示全部楼层
这是要送人进去么,等被幼幼塞爆的时候就好玩了兰空图床Lsky Pro增加AWS S3/Minio支持

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-2-9 17:37:36 | 显示全部楼层
toot 发表于 2022-2-9 12:34
本来想夸夸你。你这直接掏二维码……
实不相瞒,我是来讨饭的
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-2-9 17:38:18 | 显示全部楼层
早就应该支持了。拖到2.0才说会有。不然的话,看着后台支持的挺多。其实只有本地和FTP能实用。
回复 支持 反对

使用道具 举报

发表于 2022-2-9 17:39:04 | 显示全部楼层
似乎没有地域(Region)设置。
测试的一个MinIO上传错误。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则