记住业务流程图,要不然不清楚自己封装的是什么东西。
一、腾讯云工厂开发
切记在根目录下要安装腾讯云OSS插件,具体代码如下:
composer require qcloud/cos-sdk-v5
在根目录下extend文件夹下Upload文件夹下channel文件夹中,我们修改TencentUpload业务控制器。具体代码如下:
<?php
/**
* 腾讯云上传工厂
* User: 龙哥 三年风水
* Date: 2024/11/2/0002
* Time: 18:18
*/
namespace Upload\channel;
use Qcloud\Cos\Client;
use Upload\UploadHandle;
use Error\BaseError;
class TencentUpload extends UploadHandle
{
protected $accessKey = '';//access_key
protected $secretKey = '';//secret_key
protected $bucket = '';//包
protected $name = '';//访问地址
protected $domainName = '';//访问地址
public function __construct($param)
{
$this->accessKey = $param['access_key'];
$this->secretKey = $param['secret_key'];
$this->bucket = $param['bucket'];
$this->name = $param['name'];
$this->domainName = $param['domain_name'];
}
/**
* 上传操作
* @ param $object 加密后的文件
* @ param $filePath 临时存放的文件路径
* @ return mixed
*/
public function uploadFile($object, $filePath)
{
$ossClient = new Client(array(
'region' => 'ap-nanjing',
'schema' => 'http', //协议头部,默认为http
'credentials' => array(
'secretId' => $this->accessKey,
'secretKey' => $this->secretKey
)
));
try {
$file = fopen($filePath,'rb');
if ($file) {
$res = $ossClient->Upload($this->bucket, $object,$file);
if($res==true)return empty($this->domainName) ? $this->name.'/'.$res['Key'] : $this->domainName.'/'.$res['Key'];
}
} catch (\Exception $e) {
throw new BaseError($e->getMessage(),50000,200);
}
}
/**
* 删除操作
* @ param $object 具体的文件地址
* @ return mixed
*/
public function deleteFile($object)
{
// TODO: Implement deleteFile() method.
}
}
二、七牛云工厂开发
切记在根目录下要安装七牛云OSS插件,具体代码如下:
composer require qiniu/php-sdk
在根目录下extend文件夹下Upload文件夹下channel文件夹中,我们修改QiniuyunUpload业务控制器。具体代码如下:
<?php
/**
* 七牛云上传工厂
* User: 龙哥 三年风水
* Date: 2024/11/2/0002
* Time: 18:19
*/
namespace Upload\channel;
use Qiniu\Auth;
use Qiniu\Storage\UploadManager;
use Upload\UploadHandle;
use Error\BaseError;
class QiniuyunUpload extends UploadHandle
{
protected $accessKey = '';//access_key
protected $secretKey = '';//secret_key
protected $bucket = '';//包
protected $name = '';//访问地址
protected $domainName = '';//访问地址
public function __construct($param)
{
$this->accessKey = $param['access_key'];
$this->secretKey = $param['secret_key'];
$this->bucket = $param['bucket'];
$this->name = $param['name'];
$this->domainName = $param['domain_name'];
}
/**
* 上传操作
* @ param $object 加密后的文件
* @ param $filePath 临时存放的文件路径
* @ return mixed
*/
public function uploadFile($object, $filePath)
{
$auth = new Auth($this->accessKey,$this->secretKey);
$token = $auth->uploadToken( $this->bucket);
$uploadMgr = new UploadManager();
list($info, $error) = $uploadMgr->putFile($token, $object, $filePath);
if(!empty($error) && empty($info['key']))throw new BaseError($error,50000,200);
return $this->name.'/'.$info['key'];
}
/**
* 删除操作
* @ param $object 具体的文件地址
* @ return mixed
*/
public function deleteFile($object)
{
// TODO: Implement deleteFile() method.
}
}
三、提前说明
明天开发上传接口及调试,如果有时间的情况下,就将删除补充起来。