janfish / storage
分布式文件存储服务的客户端
v1.2
2019-07-23 10:40 UTC
Requires
- php: >=7.0.0
- ext-curl: *
This package is not auto-updated.
Last update: 2024-10-01 03:22:31 UTC
README
背景
- 图片存储作为基础服务,独立于各个应用外,通过远程调用发送图片到存储服务中。存储服务集中管理文件的创建,复制,删除以及CDN服务
客户端程序
注入phalcon
$di->set('cloudStorage', function () use ($config) { return new CloudStorage([ 'version' => 'GridFs', 'api' => 'http://disk.xy.cn/', 'imagePrefix' => 'http://cdn.xy.cn/', 'tag' => 'insurance', 'appId' => 'test', 'appSecret' => 'test123456', ]); });
- 参数配置说明
DEV测试api参数为
api: http://disk.xy.cn/
appId: test
appSecret: test123456
调用上传
单文件上传
$result = $cloudStorage->setFile($_FILES[0]['tmp_name'])->upload();
if ($result === false) {
return $app->apiResponse->error($cloudStorage->getError());
}
$images = $cloudStorage->getResult();
指定文件后缀
服务端会先使用文件后缀,如果文件没有后缀,可以通过setFile的第二参数指定后缀,如果也无,服务器会尝试使用上传文件的MIME信息判断可能的对应文件后缀
$result = $cloudStorage->setFile($_FILES[0]['tmp_name'],'.jpg)->upload();
if ($result === false) {
return $app->apiResponse->error($cloudStorage->getError());
}
$images = $cloudStorage->getResult();
返回的数据
[
{
"status": "200",
"oid": "5d29f7b04e110f00085fbab2",
"path": "insurance/201928/2332727541.jpeg",
"url": "http://cdn.xy.cn/insurance/201928/2332727541.jpeg"
}
]
指定tag上传
$result = $cloudStorage->setFile($_FILES[0]['tmp_name'])->setTag('test')->upload();
if ($result === false) {
return $app->apiResponse->error($cloudStorage->getError());
}
$images = $cloudStorage->getResult();
批量上传
这里无法指定文件后缀,所以设置的上传文件最好本身带有后缀
$cloudStorage = $app->cloudStorage;
$files = array_column($_FILES, 'tmp_name');
$cloudStorage->setFiles($files);
$cloudStorage->setTag('insurance');
if ($cloudStorage->upload() === false) {
echo $cloudStorage->getError();
}
$images = $cloudStorage->getResult();
返回的数据
[
{
"status": "200",
"oid": "5d29f7b04e110f00085fbab2",
"path": "insurance/201928/2332727541.jpeg",
"url": "http://cdn.xy.cn/insurance/201928/2332727541.jpeg"
},
{
"status": "200",
"oid": "5d29f7b04e110f00085fbab2",
"path": "insurance/201928/106785628.jpeg",
"url": "http://cdn.xy.cn/insurance/201928/106785628.jpeg"
}
]
本地约束限制设置(服务端针对appid也有限制,权限应该在服务端的限制之下)
$result = $app->cloudStorage->setAllowed([
'image/jpeg',
'image/jpg',
'image/png',
'application/zip',
'application/x-rar',
'application/x-zip-compressed',
])
->setMaxSize(5)
->setFiles(array_column($_FILES, 'tmp_name'))
->setTag('insurance')->upload();
if ($result === false) {
echo $app->cloudStorage->getError()
}
删除文件
$cloudStorage = $app->cloudStorage; if ($cloudStorage->remove(['insurance/201928/106785628.jpeg','insurance/201928/2332727541.jpeg']) === false) { echo $cloudStorage->getError(); }
调用地址
获取CDN地址
上传接口返回的path一般本地存取,通过拼装函数获得最终CDN地址
echo $app->cloudStorage->getStaticUrl('insurance/201928/2332727541.jpeg');
输出数据
http://cdn.xy.cn/insurance/201928/2332727541.jpeg
图片在线裁剪(需要CDN支持)
echo $app->cloudStorage->getStaticUrl('insurance/201928/2332727541.jpeg',['width'=>100,'height'=>100,clipType='1']);