一、阿里云

/**
* Created by PhpStorm.
* User: Mr.Yang
* Date: 2022/4/13
* Time: 10:14
* QQ: 2575404985
*/

namespace app\api\controller;

use OSS\OssClient;
use OSS\Core\OssException;

require_once __DIR__ . '/../../../vendor/alioss/autoload.php';

class Oss {
private $ossClient = null;
private $accessKeyId = null;
private $accessKeySecret = null;
private $endpoint = null;
private $bucket = null;
public function __construct($accessKeyId, $accessKeySecret, $endpoint, $bucket)
{
$this->accessKeyId = $accessKeyId;
$this->accessKeySecret = $accessKeySecret;
$this->endpoint = $endpoint;
$this->bucket = $bucket;
if (!$this->ossClient){
try{
$this->ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
} catch(OssException $e) {
Send::returnMsg(-2, $e->getMessage());
}
}
}

/**
* 上传文件
* @param $path
* @return null
* @throws OssException
*/
public function uploadFile($path)
{
$this->ossClient->uploadFile($this->bucket, $path, __ROOT__.'/'.$path);
@unlink(__ROOT__.'/'.$path);
return true;
}

/**
* 获取bucket列表
* @return array
* @throws OssException
*/
public function getBucketList()
{
$bucketListInfo = $this->ossClient->listBuckets();
$bucketList = $bucketListInfo->getBucketList();
$bucket_array = array();
foreach($bucketList as $bucket) {
$name = $bucket->getName();
$location = $bucket->getLocation();
$bucket_array[] = array(
'name' => $bucket->getName(),
'bucket' => $name.'@'.$location
);
}
return $bucket_array;
}

/**
* 删除文件
* @param $path
* @return null
*/
public function delFile($path)
{
return $this->ossClient->deleteObject($this->bucket, $path);
}
}

二、七牛云

/**
* Created by PhpStorm.
* User: Mr.Yang
* Date: 2022/4/13
* Time: 14:33
* QQ: 2575404985
*/

namespace app\api\controller;

use Qiniu\Auth;
use Qiniu\Config;
use Qiniu\Storage\BucketManager;
use Qiniu\Storage\UploadManager;

require_once __DIR__ . '/../../../vendor/qiniu/autoload.php';
class Qiniu
{
private $accesskey = null;
private $secretkey = null;
private $bucket = null;
private $auth = null;

public function __construct($accesskey, $secretkey, $bucket)
{
$this->accesskey = $accesskey;
$this->secretkey = $secretkey;
$this->bucket = $bucket;
if (!$this->auth){
$this->auth = new Auth($accesskey, $secretkey);
}
}

/**
* 删除远程文件
* @param $path
* @return bool
* @throws \Exception
*/
public function uploadFile($path)
{
// 生成上传 Token
$token = $this->auth->uploadToken($this->bucket);
$uploadMgr = new UploadManager();
$filePath = __ROOT__.'/'.$path;
list($ret, $err) = $uploadMgr->putFile($token, $path, $filePath);
if ($err !== null) {
return false;
} else {
@unlink(__ROOT__.'/'.$path);
return true;
}
}

/**
* 删除远程文件
* @param $path
* @return array
*/
public function delFile($path)
{
$config = new Config();
$bucketManager = new BucketManager($this->auth, $config);
return $bucketManager->delete($this->bucket, $path);
}
}

三、腾讯云

/**
* Created by PhpStorm.
* User: Mr.Yang
* Date: 2022/4/13
* Time: 11:11
* QQ: 2575404985
*/

namespace app\api\controller;

require_once __DIR__ . '/../../../vendor/tencent-cos/index.php';

class Cos
{
private $cosClient = null;
private $secretId = null;
private $secretKey = null;
private $region = null;
private $bucket = null;
private $host = null;

public function __construct($secretId, $secretKey, $region, $bucket, $host = '')
{

$this->secretId = $secretId; //替换为用户的 secretId,请登录访问管理控制台进行查看和管理,https://console.cloud.tencent.com/cam/capi
$this->secretKey = $secretKey; //替换为用户的 secretKey,请登录访问管理控制台进行查看和管理,https://console.cloud.tencent.com/cam/capi
$this->region = $region; //替换为用户的 region,已创建桶归属的region可以在控制台查看,https://console.cloud.tencent.com/cos5/bucket
$this->bucket = $bucket; //存储桶名称,由BucketName-Appid 组成,可以在COS控制台查看 https://console.cloud.tencent.com/cos5/bucket
$this->host = $host;

if (!$this->cosClient) {
$config = array(
//'domain' => $host,
'region' => $this->region,
'schema' => 'http', //协议头部,默认为http
'credentials' => array(
'secretId' => $this->secretId,
'secretKey' => $this->secretKey
)
);
$this->cosClient = new \Qcloud\Cos\Client($config);
}
}

/**
* 上传文件
* @param $path
* @return object
*/
public function uploadFile($path)
{
$this->cosClient->putObject(array(
'Bucket' => $this->bucket, //存储桶名称,由BucketName-Appid 组成,可以在COS控制台查看 https://console.cloud.tencent.com/cos5/bucket
'Key' => $path,
'Body' => fopen(__ROOT__ . '/' . $path, 'rb')
));
//删除本地文件
@unlink(__ROOT__.'/'.$path);
}

/**
* 删除文件
* @param $path
* @return object
*/
public function delFile($path)
{
return $this->cosClient->deleteObject(array(
'Bucket' => $this->bucket, //存储桶名称,由BucketName-Appid 组成,可以在COS控制台查看 https://console.cloud.tencent.com/cos5/bucket
'Key' => $path
));
}
}