123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- <?php
- // +----------------------------------------------------------------------
- // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
- // +----------------------------------------------------------------------
- // | Author: 萤火科技 <admin@yiovo.com>
- // +----------------------------------------------------------------------
- declare (strict_types = 1);
- namespace app\common\library\storage\engine;
- use app\common\library\storage\FileValidate;
- use OSS\OssClient;
- use OSS\Core\OssException;
- use think\facade\Env;
- /**
- * 阿里云存储引擎 (OSS)
- * Class Aliyun
- * @package app\common\library\storage\engine
- */
- class Aliyun extends Basics
- {
- /**
- * 执行上传
- * @return bool|mixed
- */
- public function upload()
- {
- if (!$this->validate()) {
- return false;
- }
- try {
- $ossClient = new OssClient(
- $this->config['access_key_id'],
- $this->config['access_key_secret'],
- $this->config['domain'],
- true
- );
- $result = $ossClient->uploadFile(
- $this->config['bucket'],
- $this->getSaveFileInfo()['file_path'],
- $this->getRealPath()
- );
- } catch (OssException $e) {
- $this->error = $e->getMessage();
- return false;
- }
- return true;
- }
- /**
- * 删除文件
- * @param string $fileName
- * @return bool|mixed
- */
- public function delete(string $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;
- }
- /**
- * 下载文件
- * @param $object
- * @return false|string
- */
- public function download($object){
- // 下载Object到本地文件examplefile.txt,并保存到指定的本地路径中(D:\\localpath)。如果指定的本地文件存在会覆盖,不存在则新建。
- // 如果未指定本地路径,则下载后的文件默认保存到示例程序所属项目对应本地路径中。
- $exts = explode('.',$object);
- $localfile = runtime_root_path().mt_rand(10000000,99999999).'.'.$exts[1];
- $options = array(
- OssClient::OSS_FILE_DOWNLOAD => $localfile
- );
- // 使用try catch捕获异常。如果捕获到异常,则说明下载失败;如果没有捕获到异常,则说明下载成功。
- try{
- $ossClient = new OssClient(
- $this->config['access_key_id'],
- $this->config['access_key_secret'],
- Env::get('CHEF.SSO_END_POINT')
- );
- $ossClient->getObject($this->config['bucket'], $object, $options);
- } catch(OssException $e) {
- //printf(__FUNCTION__ . ": FAILED\n");
- //printf($e->getMessage() . "\n");
- log_record($e->getMessage());
- return false;
- }
- return $localfile;
- }
- /**
- * 验证上传的文件
- * @return bool
- */
- private function validate()
- {
- $FileValidate = new FileValidate;
- if (!$FileValidate->check([$this->validateRuleScene => $this->file])) {
- $this->error = $FileValidate->getError();
- return false;
- }
- return true;
- }
- }
|