全球主机交流论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

CeraNetworks网络延迟测速工具IP归属甄别会员请立即修改密码
查看: 1733|回复: 14

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

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

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

大约如下几步:

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

  1. <?php
  2. namespace strategy\driver;
  3. use strategy\Driver;
  4. if (!function_exists('exif_imagetype')) {
  5.     function exif_imagetype($filename)
  6.     {
  7.         if ((list($width, $height, $type, $attr) = getimagesize($filename)) !== false) {
  8.             return $type;
  9.         }
  10.         return false;
  11.     }
  12. }
  13. /**
  14. * Aws储存驱动
  15. *
  16. * Class Aws
  17. * @package strategy\driver
  18. */
  19. class Aws implements Driver
  20. {
  21.     /**
  22.      * 当前储存策略参数
  23.      *
  24.      * [url=home.php?mod=space&uid=32455]@var[/url] array
  25.      */
  26.     protected $options = [];
  27.     /**
  28.      * 错误信息
  29.      *
  30.      * @var null
  31.      */
  32.     protected $error = null;
  33.     /**
  34.      * Aws实例
  35.      *
  36.      * @var null
  37.      */
  38.     protected $s3 = null;
  39.     /**
  40.      * Aws constructor.
  41.      *
  42.      * @param array $options
  43.      */
  44.     public function __construct($options = [])
  45.     {
  46.         $this->options = $options;
  47.         try {
  48.             $this->s3 = new \Aws\S3\S3Client([
  49.                 'version' => 'latest',
  50.                 'region'  => empty($this->options['s3_region']) ? 'us-east-1' : $this->options['s3_region'],
  51.                 'endpoint' => $this->options['s3_endpoint'],
  52.                 'use_path_style_endpoint' => true,
  53.                 'credentials' => [
  54.                     'key'    => $this->options['s3_key'],
  55.                     'secret' => $this->options['s3_secret'],
  56.                 ],
  57.             ]);
  58.         } catch (\Exception $e) {
  59.             $this->error = $e->getMessage();
  60.         }
  61.     }
  62.     /**
  63.      * 创建文件
  64.      *
  65.      * @param $pathname
  66.      * @param $file
  67.      *
  68.      * @return bool
  69.      */
  70.     public function create($pathname, $file)
  71.     {
  72.         try {
  73.             $params = array(
  74.                 'Bucket' => $this->options['s3_bucket'],
  75.                 'Key' => $pathname,
  76.                 'Body' => fopen($file, 'rb')
  77.             );
  78.             if ($image_type = exif_imagetype($file)) {
  79.                 $params['ContentType'] = image_type_to_mime_type($image_type);
  80.             }
  81.             $this->s3->putObject($params);
  82.         } catch (\Exception $e) {
  83.             $this->error = $e->getMessage();
  84.             return false;
  85.         }
  86.         return true;
  87.     }
  88.     /**
  89.      * 删除文件
  90.      *
  91.      * @param $pathname
  92.      *
  93.      * @return bool
  94.      */
  95.     public function delete($pathname)
  96.     {
  97.         try {
  98.             $this->s3->deleteObject([
  99.                 'Bucket' => $this->options['s3_bucket'],
  100.                 'Key' => $pathname,
  101.             ]);
  102.         } catch (\Exception $e) {
  103.             $this->error = $e->getMessage();
  104.             return false;
  105.         }
  106.         return true;
  107.     }
  108.     /**
  109.      * 删除多个文件
  110.      *
  111.      * @param array $list
  112.      * @return bool|mixed
  113.      */
  114.     public function deletes(array $list)
  115.     {
  116.         try {
  117.             $objects = [];
  118.             foreach ($list as $value) {
  119.                 $objects[] = ['Key' => $value ];
  120.             }
  121.             $this->s3->deleteObjects([
  122.                 'Bucket' => $this->options['s3_bucket'],
  123.                 'Objects' => $objects,
  124.             ]);
  125.         } catch (\Exception $e) {
  126.             $this->error = $e->getMessage();
  127.             return false;
  128.         }
  129.         return true;
  130.     }
  131.     public function getError()
  132.     {
  133.         return 'Aws:' . $this->error;
  134.     }
  135. }
复制代码
二、增加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 12:33:06 | 显示全部楼层
他不是在做2.0吗,等2.0这些都支持了
发表于 2022-2-9 12:33:47 | 显示全部楼层
前排支持





asbd zsbd
发表于 2022-2-9 12:34:01 | 显示全部楼层
本来想夸夸你。你这直接掏二维码……
 楼主| 发表于 2022-2-9 12:34:10 来自手机 | 显示全部楼层
海苔 发表于 2022-2-9 12:33
他不是在做2.0吗,等2.0这些都支持了

看第一行和第二行
发表于 2022-2-9 12:34:19 | 显示全部楼层
说得好像1.0和2.0不互通。这样的话搭建2.0数据就没了。
发表于 2022-2-9 12:35:37 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽
 楼主| 发表于 2022-2-9 12:38:27 来自手机 | 显示全部楼层
toot 发表于 2022-2-9 12:34
本来想夸夸你。你这直接掏二维码……

实不相瞒,我是来讨饭的
发表于 2022-2-9 15:04:40 | 显示全部楼层
早就应该支持了。拖到2.0才说会有。不然的话,看着后台支持的挺多。其实只有本地和FTP能实用。
发表于 2022-2-9 16:06:58 | 显示全部楼层
rooney 发表于 2022-2-9 12:38
实不相瞒,我是来讨饭的

似乎没有地域(Region)设置。
测试的一个MinIO上传错误。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|全球主机交流论坛

GMT+8, 2024-4-26 01:20 , Processed in 0.063103 second(s), 8 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表