对文件存储的轻量封装,屏蔽不同云环境api差异,方便开发和部署时随意切换存储环境
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了对文件存储的轻量封装,屏蔽不同云环境api差异,方便开发和部署时随意切换存储环境相关的知识,希望对你有一定的参考价值。
文件存储
安装
composer require pfinal/storage
-
本地存储 Local
-
阿里云 Alioss
请先 composer require aliyuncs/oss-sdk-php
-
7牛存诸 Qiniu
请先 composer require qiniu/php-sdk
-
Ftp
请先 composer league/flysystem
提供接口
//上传文件
public function put($key, $data);
//获取url
public function url($key, $rule = null);
//重命名
public function rename($key, $newKey);
//删除
public function delete($key);
//获取错误消息
public function error();
使用示例
<?php
require_once __DIR__ . ‘/vendor/autoload.php‘;
//7牛
$config = array(
‘accessKey‘ => ‘TOeV-fwwxsssf3s_45tCziKjRD9-bPyXUKjbuX7b‘,
‘secretKey‘ => ‘pbHrgwwwp_wpClxeeGrYKLNdEhLd02Jrew3t5h‘,
‘bucketName‘ => ‘test‘,
‘baseUrl‘ => ‘http://static.pfinal.cn/‘,
‘separator‘ => ‘-‘,
);
$qiniu = new \PFinal\Storage\Qiniu($config);
$bool = $qiniu->put(‘test/1.jpg‘, file_get_contents(‘/Users/ethan/Pictures/1.jpg‘));
//原图url
var_dump($qiniu->url(‘test/1.jpg‘));
//小图url 规则: "m"
var_dump($qiniu->url(‘test/1.jpg‘, ‘m‘));
//阿里云OSS
$config = [
‘accessKey‘ => ‘your key‘,
‘secret‘ => ‘your secret‘,
‘endPoint‘ => ‘oss-cn-shanghai.aliyuncs.com‘,
‘bucket‘ => ‘your bucket‘,
];
$oss = new \PFinal\Storage\AliOss($config);
$bool = $oss->put(‘test.jpg‘, file_get_contents(‘/Users/ethan/Pictures/1.jpg‘));
//原图url
echo $oss->url(‘test.jpg‘);
//小图url 规则名称: "s"
echo $oss->url(‘test.jpg‘, ‘s‘);
https://github.com/pfinal/storage
以上是关于对文件存储的轻量封装,屏蔽不同云环境api差异,方便开发和部署时随意切换存储环境的主要内容,如果未能解决你的问题,请参考以下文章