提交 ad980a71 编写于 作者: 李光春's avatar 李光春

- 增加对象存储

上级 b81ede02
......@@ -55,6 +55,13 @@ return [
// 地图
'lbs' => [
'ak' => ''
],
// 百度云
'bos' => [
'access_key_id' => '',
'secret_access_key' => '',
'endpoint' => '',
'bucket' => '',
]
],
// 高德地图
......@@ -100,5 +107,32 @@ return [
// 服务器IP(节点)
'ip' => ''
]
]
],
// 阿里云
'aliyun' => [
'oss' => [
'access_key_id' => '',
'access_key_secret' => '',
'endpoint' => '',
'bucket' => '',
]
],
// 腾讯云
'tencent' => [
'cos' => [
'secret_id' => '',
'secret_key' => '',
'region' => '',
'bucket' => '',
]
],
// 华为云
'huaweicloud' => [
'obs' => [
'key' => '',
'secret' => '',
'endpoint' => '',
'bucket' => '',
]
],
];
......@@ -17,8 +17,76 @@
namespace DtApp\ThinkLibrary\service\aliyun;
use DtApp\ThinkLibrary\Service;
use OSS\Core\OssException;
use OSS\OssClient;
/**
* 阿里云对象存储
* Class OssService
* @package DtApp\ThinkLibrary\service\aliyun
*/
class OssService extends Service
{
private $accessKeyId;
private $accessKeySecret;
private $endpoint;
private $bucket;
public function accessKeyId(string $accessKeyId)
{
$this->accessKeyId = $accessKeyId;
return $this;
}
public function accessKeySecret(string $accessKeySecret)
{
$this->accessKeySecret = $accessKeySecret;
return $this;
}
public function endpoint(string $endpoint)
{
$this->endpoint = $endpoint;
return $this;
}
public function bucket(string $bucket)
{
$this->bucket = $bucket;
return $this;
}
/**
* 获取配置信息
* @return $this
*/
private function getConfig()
{
$this->accessKeyId = $this->app->config->get('dtapp.aliyun.oss.access_key_id');
$this->accessKeySecret = $this->app->config->get('dtapp.aliyun.oss.access_key_secret');
$this->endpoint = $this->app->config->get('dtapp.aliyun.oss.endpoint');
$this->bucket = $this->app->config->get('dtapp.aliyun.oss.bucket');
return $this;
}
/**
* 上传文件
* @param $object
* @param $filePath
* @return bool|string
*/
public function upload($object, $filePath)
{
if (empty($this->accessKeySecret)) $this->getConfig();
if (empty($this->accessKeySecret)) $this->getConfig();
if (empty($this->endpoint)) $this->getConfig();
if (empty($this->bucket)) $this->getConfig();
try {
$ossClient = new OssClient($this->accessKeyId, $this->accessKeySecret, $this->endpoint);
$ossClient->uploadFile($this->bucket, $object, $filePath);
return true;
} catch (OssException $e) {
return $e->getMessage();
}
}
}
<?php
// +----------------------------------------------------------------------
// | ThinkLibrary 6.0 for ThinkPhP 6.0
// +----------------------------------------------------------------------
// | 版权所有 2017~2020 [ https://www.dtapp.net ]
// +----------------------------------------------------------------------
// | 官方网站: https://gitee.com/liguangchun/ThinkLibrary
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | gitee 仓库地址 :https://gitee.com/liguangchun/ThinkLibrary
// | github 仓库地址 :https://github.com/GC0202/ThinkLibrary
// | Packagist 地址 :https://packagist.org/packages/liguangchun/think-library
// +----------------------------------------------------------------------
namespace DtApp\ThinkLibrary\service\baidu;
use BaiduBce\Services\Bos\BosClient;
use DtApp\ThinkLibrary\Service;
use Exception;
/**
* 百度云对象存储
* Class BosService
* @package DtApp\ThinkLibrary\service\baidu
*/
class BosService extends Service
{
private $accessKeyId;
private $secretAccessKey;
private $endpoint;
private $bucket;
public function accessKeyId(string $accessKeyId)
{
$this->accessKeyId = $accessKeyId;
return $this;
}
public function secretAccessKey(string $secretAccessKey)
{
$this->secretAccessKey = $secretAccessKey;
return $this;
}
public function endpoint(string $endpoint)
{
$this->endpoint = $endpoint;
return $this;
}
public function bucket(string $bucket)
{
$this->bucket = $bucket;
return $this;
}
/**
* 获取配置信息
* @return $this
*/
private function getConfig()
{
$this->accessKeyId = $this->app->config->get('dtapp.baidu.bos.access_key_id');
$this->secretAccessKey = $this->app->config->get('dtapp.baidu.bos.secret_access_key');
$this->endpoint = $this->app->config->get('dtapp.baidu.bos.endpoint');
$this->bucket = $this->app->config->get('dtapp.baidu.bos.bucket');
return $this;
}
/**
* 上传文件
* @param $object
* @param $filePath
* @return bool
* @throws Exception
*/
private function upload($object, $filePath)
{
if (empty($this->accessKeyId)) $this->getConfig();
if (empty($this->secretAccessKey)) $this->getConfig();
if (empty($this->endpoint)) $this->getConfig();
// 设置BosClient的Access Key ID、Secret Access Key和ENDPOINT
$BOS_TEST_CONFIG = array(
'credentials' => array(
'accessKeyId' => $this->accessKeyId,
'secretAccessKey' => $this->secretAccessKey,
),
'endpoint' => $this->endpoint,
);
$client = new BosClient($BOS_TEST_CONFIG);
// 从文件中直接上传Object
if (empty($this->bucket)) $this->getConfig();
$client->putObjectFromFile($this->bucket, $object, $filePath);
return true;
}
}
<?php
// +----------------------------------------------------------------------
// | ThinkLibrary 6.0 for ThinkPhP 6.0
// +----------------------------------------------------------------------
// | 版权所有 2017~2020 [ https://www.dtapp.net ]
// +----------------------------------------------------------------------
// | 官方网站: https://gitee.com/liguangchun/ThinkLibrary
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | gitee 仓库地址 :https://gitee.com/liguangchun/ThinkLibrary
// | github 仓库地址 :https://github.com/GC0202/ThinkLibrary
// | Packagist 地址 :https://packagist.org/packages/liguangchun/think-library
// +----------------------------------------------------------------------
namespace DtApp\ThinkLibrary\service\huaweicloud;
use DtApp\ThinkLibrary\Service;
use Obs\ObsClient;
/**
* 华为云对象存储
* Class ObsService
* @package DtApp\ThinkLibrary\service\huaweicloud
*/
class ObsService extends Service
{
private $key;
private $secret;
private $endpoint;
private $bucket;
public function key(string $key)
{
$this->key = $key;
return $this;
}
public function secret(string $secret)
{
$this->secret = $secret;
return $this;
}
public function endpoint(string $endpoint)
{
$this->endpoint = $endpoint;
return $this;
}
public function bucket(string $bucket)
{
$this->bucket = $bucket;
return $this;
}
/**
* 获取配置信息
* @return $this
*/
private function getConfig()
{
$this->key = $this->app->config->get('dtapp.huaweicloud.obs.key');
$this->secret = $this->app->config->get('dtapp.huaweicloud.obs.secret');
$this->endpoint = $this->app->config->get('dtapp.huaweicloud.obs.endpoint');
$this->bucket = $this->app->config->get('dtapp.huaweicloud.obs.bucket');
return $this;
}
/**
* 上传到华为云
* @param $object
* @param $filePath
* @return bool
*/
private function upload($object, $filePath)
{
if (empty($this->key)) $this->getConfig();
if (empty($this->secret)) $this->getConfig();
if (empty($this->endpoint)) $this->getConfig();
// 创建ObsClient实例
$obsClient = new ObsClient([
'key' => $this->key,
'secret' => $this->secret,
'endpoint' => $this->endpoint
]);
if (empty($this->bucket)) $this->getConfig();
$resp = $obsClient->putObject([
'Bucket' => $this->bucket,
'Key' => $object,
'SourceFile' => $filePath // localfile为待上传的本地文件路径,需要指定到具体的文件名
]);
if (isset($resp['RequestId'])) {
return true;
} else {
return false;
}
}
}
<?php
// +----------------------------------------------------------------------
// | ThinkLibrary 6.0 for ThinkPhP 6.0
// +----------------------------------------------------------------------
// | 版权所有 2017~2020 [ https://www.dtapp.net ]
// +----------------------------------------------------------------------
// | 官方网站: https://gitee.com/liguangchun/ThinkLibrary
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | gitee 仓库地址 :https://gitee.com/liguangchun/ThinkLibrary
// | github 仓库地址 :https://github.com/GC0202/ThinkLibrary
// | Packagist 地址 :https://packagist.org/packages/liguangchun/think-library
// +----------------------------------------------------------------------
namespace DtApp\ThinkLibrary\service\tencent;
use DtApp\ThinkLibrary\Service;
use Exception;
use Qcloud\Cos\Client;
/**
* 腾讯云对象存储
* Class CosService
* @package DtApp\ThinkLibrary\service\tencent
*/
class CosService extends Service
{
private $secretId;
private $secretKey;
private $region;
private $bucket;
public function secretId(string $secretId)
{
$this->secretId = $secretId;
return $this;
}
public function secretKey(string $secretKey)
{
$this->secretKey = $secretKey;
return $this;
}
public function region(string $region)
{
$this->region = $region;
return $this;
}
public function bucket(string $bucket)
{
$this->bucket = $bucket;
return $this;
}
/**
* 获取配置信息
* @return $this
*/
private function getConfig()
{
$this->secretId = $this->app->config->get('dtapp.tencent.cos.secret_id');
$this->secretKey = $this->app->config->get('dtapp.tencent.cos.secret_key');
$this->region = $this->app->config->get('dtapp.tencent.cos.region');
$this->bucket = $this->app->config->get('dtapp.tencent.cos.bucket');
return $this;
}
/**
* 上传文件
* @param $object
* @param $filePath
* @return bool
* @throws Exception
*/
private function upload($object, $filePath)
{
if (empty($this->secretId)) $this->getConfig();
if (empty($this->secretKey)) $this->getConfig();
if (empty($this->region)) $this->getConfig();
$cosClient = new Client(
array(
'region' => $this->region,
'schema' => 'http', //协议头部,默认为http
'credentials' => array(
'secretId' => $this->secretId,
'secretKey' => $this->secretKey
)
)
);
$key = $object;
$file = fopen($filePath, "rb");
if ($file) {
if (empty($this->bucket)) $this->getConfig();
$result = $cosClient->putObject(
array(
'Bucket' => $this->bucket,
'Key' => $key,
'Body' => $file)
);
}
return true;
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册