You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
think-library/src/service/aliyun/OssService.php

112 lines
3.4 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?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
// | gitlab 仓库地址 https://gitlab.com/liguangchun/thinklibrary
// | weixin 仓库地址 https://git.weixin.qq.com/liguangchun/ThinkLibrary
// | huaweicloud 仓库地址 https://codehub-cn-south-1.devcloud.huaweicloud.com/composer00001/ThinkLibrary.git
// | Packagist 地址 https://packagist.org/packages/liguangchun/think-library
// +----------------------------------------------------------------------
namespace DtApp\ThinkLibrary\service\aliyun;
use DtApp\ThinkLibrary\Service;
use OSS\Core\OssException;
use OSS\OssClient;
/**
* 阿里云对象存储
* https://www.aliyun.com/product/oss
* Class OssService
* @package DtApp\ThinkLibrary\service\aliyun
*/
class OssService extends Service
{
/**
* @var
*/
private $accessKeyId, $accessKeySecret, $endpoint, $bucket;
/**
* @param string $accessKeyId
* @return $this
*/
public function accessKeyId(string $accessKeyId)
{
$this->accessKeyId = $accessKeyId;
return $this;
}
/**
* @param string $accessKeySecret
* @return $this
*/
public function accessKeySecret(string $accessKeySecret)
{
$this->accessKeySecret = $accessKeySecret;
return $this;
}
/**
* @param string $endpoint
* @return $this
*/
public function endpoint(string $endpoint): self
{
$this->endpoint = $endpoint;
return $this;
}
/**
* @param string $bucket
* @return $this
*/
public function bucket(string $bucket): self
{
$this->bucket = $bucket;
return $this;
}
/**
* 获取配置信息
* @return $this
*/
private function getConfig(): self
{
$this->accessKeyId = config('dtapp.aliyun.oss.access_key_id');
$this->accessKeySecret = config('dtapp.aliyun.oss.access_key_secret');
$this->endpoint = config('dtapp.aliyun.oss.endpoint');
$this->bucket = config('dtapp.aliyun.oss.bucket');
return $this;
}
/**
* 上传文件
* @param $object
* @param $filePath
* @return bool|string
*/
public function upload(string $object, string $filePath)
{
if (empty($this->accessKeySecret) || empty($this->accessKeySecret) || empty($this->endpoint) || empty($this->bucket)) {
$this->getConfig();
}
try {
$ossClient = new OssClient($this->accessKeyId, $this->accessKeySecret, $this->endpoint);
$ossClient->uploadFile($this->bucket, $object, $filePath);
return config('dtapp.aliyun.oss.url', '') . $object;
} catch (OssException $e) {
return $e->getMessage();
}
}
}