<?php
|
|
namespace app\common\library\storage\engine;
|
|
use OSS\OssClient;
|
use OSS\Core\OssException;
|
use think\facade\Filesystem;
|
|
/**
|
* 阿里云存储引擎 (OSS)
|
*/
|
class Aliyun extends Server
|
{
|
private $config;
|
|
/**
|
* 构造方法
|
*/
|
public function __construct($config)
|
{
|
parent::__construct();
|
$this->config = $config;
|
}
|
|
/**
|
* 执行上传
|
*/
|
public function upload()
|
{
|
try {
|
// 扩展名
|
$ext = $this->getOriginalExtension();
|
$saveName = date('Ymd').'/'.md5((string) microtime(true)).".{$ext}";
|
//$saveName = Filesystem::disk('public')->putFile( '', $this->file);
|
$ossClient = new OssClient(
|
$this->config['access_key_id'],
|
$this->config['access_key_secret'],
|
$this->config['domain'],
|
true
|
);
|
// 创建目录
|
$ossClient->createObjectDir($this->config['bucket'], date('Ymd'));
|
// 上传文件
|
$ossClient->uploadFile(
|
$this->config['bucket'],
|
$saveName,
|
$this->getRealPath()
|
);
|
} catch (OssException $e) {
|
$this->error = $e->getMessage();
|
return false;
|
}
|
return $saveName;
|
}
|
|
/**
|
* 删除文件
|
*/
|
public function delete($fileName)
|
{
|
try {
|
$ossClient = new OssClient(
|
$this->config['access_key_id'],
|
$this->config['access_key_secret'],
|
$this->config['domain'],
|
true
|
);
|
$ossClient->deleteObject($this->config['bucket'], $fileName);
|
} catch (OssException $e) {
|
$this->error = $e->getMessage();
|
return false;
|
}
|
return true;
|
}
|
|
/**
|
* 返回文件路径
|
*/
|
public function getFileName()
|
{
|
return $this->fileName;
|
}
|
|
}
|