最近由于业务增长,图片请求量增加,导致某云的云存储费用增加,后来联系了白山对比价格,相对会减少一部分开支,于是就开始着手对接白山云存储业务。
首先查看白山云存储开发文档,完全兼容aws接口文档,考虑到使用maccms
的便捷和统一性,就在原有的后台管理中添加新的存储方式。aws有专门的PHP sdk包,可以通过composer或者下载安装包的方式进行集成,下面介绍下composer方式。
https://docs.aws.amazon.com/zh_cn/sdk-for-php/v3/developer-guide/guide_configuration.html
在项目中执行
composer require aws/aws-sdk-php
然后在maccms后台找到附件配置相关的控制器方法,比葫芦画瓢,对照着原有的三方存储,增加扩展文件和视图文件即可,可以参考下面代码:
<?php
namespace app\common\extend\upload;
use Aws\S3\S3Client;
use Aws\Exception\AwsException;
class Aws
{
public $name = 'AWS云存储';
public $ver = '3.0';
public function submit($file_path)
{
$bucket = $GLOBALS['config']['upload']['api']['aws']['bucket'];
$accessKey = $GLOBALS['config']['upload']['api']['aws']['accesskey'];
$secretKey = $GLOBALS['config']['upload']['api']['aws']['secretkey'];
$endpoint = $GLOBALS['config']['upload']['api']['aws']['url'];
$s3 = new S3Client([
'version' => 'latest',
'region' => 'us-east-2',
'credentials' => [
'key' => $accessKey, //请替换为您自己的access_key
'secret' => $secretKey, //请替换为您自己的secret_key
],
'endpoint' => $endpoint
]);
$imgName = date('Y',time()).'/'.date('m',time()).'/'.mac_get_uniqid_code().'.'.pathinfo($file_path)['extension'];
$filePath = ROOT_PATH . $file_path;
$resp = $s3->putObject([
'ACL' => 'public-read',
'Bucket' => $bucket,
'Key' => $imgName,//文件名
'ContentType' => 'image/jpeg', //请替换为合适的文件类型
//'Body' => 'file content as a string',
'SourceFile' => $file_path//'/root/test.jpg',
]);
@unlink($filePath);
return $resp['ObjectURL'];
}
}
//文件路径
application/common/extend/upload/Aws.php
接下来是视图文件
//application/admin/view/extend/upload/aws.html
<div class="layui-form-item upload_mode mode_Aws" {if condition="$config['upload']['mode'] neq 'Aws'"}style="display:none;" {/if}>
<label class="layui-form-label">AWS API:</label>
<div class="layui-input-block">
<a href="https://aws.amazon.com/cn/documentation/sdk-for-php/" target="_blank" class="layui-btn layui-btn-primary">点击申请 https://aws.amazon.com/cn/documentation/sdk-for-php/</a>
</div>
</div>
<div class="layui-form-item upload_mode mode_Aws" {if condition="$config['upload']['mode'] neq 'Aws'"}style="display:none;" {/if}>
<label class="layui-form-label">服务名:</label>
<div class="layui-input-inline w200">
<input type="text" name="upload[api][aws][bucket]" placeholder="白山云-Bucket" value="{$config['upload']['api']['aws']['bucket']}" class="layui-input" >
</div>
<label class="layui-form-label">access_key:</label>
<div class="layui-input-inline w200">
<input type="text" name="upload[api][aws][accesskey]" placeholder="白山云-access_key" value="{$config['upload']['api']['aws']['accesskey']}" class="layui-input" >
</div>
<label class="layui-form-label">secretkey:</label>
<div class="layui-input-inline w200">
<input type="text" name="upload[api][aws][secretkey]" placeholder="白山云-secretkey" value="{$config['upload']['api']['aws']['secretkey']}" class="layui-input" >
</div>
<label class="layui-form-label">endpoint:</label>
<div class="layui-input-inline w200">
<input type="text" name="upload[api][aws][url]" placeholder="白山云-endpoint" value="{$config['upload']['api']['aws']['url']}" class="layui-input" >
</div>
</div>
然后登录maccms管理后台填写相应的配置,测试即可完成。