| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- <?php
- /**
- * Created by PhpStorm.
- * User: weifeng
- * Date: 2020-05-11
- * Time: 16:18
- *
- * .--, .--,
- * ( ( \.---./ ) )
- * '.__/o o\__.'
- * {= ^ =}
- * > - <
- * / \
- * // \\
- * //| . |\\
- * "'\ /'"_.-~^`'-.
- * \ _ /--' `
- * ___)( )(___
- * (((__) (__))) 梦之所想,心之所向.
- */
- namespace app\common\services;
- use app\platform\modules\system\models\SystemSetting;
- class QcloudCosService
- {
- private $app_id;
- private $region;
- private $secretId;
- private $secretKey;
- private $bucket;
- private $cosClient;
- public function __construct($region = '', $secretId = '', $secretKey = '', $bucket = '', $appid = '')
- {
- $this->app_id = $appid;
- $this->region = $region; //地域
- $this->secretId = $secretId;
- $this->secretKey = $secretKey;
- $this->bucket = $bucket . '-' . $appid;
- $this->cosClient = CosV5Service::init($this->region, $this->secretId, $this->secretKey);
- }
- /**
- * 上传文件流 测试
- * @param $bucket
- * @param $key
- * @param $srcPath
- * @param $cosClient
- */
- public function uploadTest($key = '')
- {
- $file = request()->getSchemeAndHttpHost() . '/static/' . $key;
- try {
- if ($file) {
- $result = $this->cosClient->putObject(array(
- 'Bucket' => $this->bucket,
- 'Key' => $key,
- 'Body' => $this->curl_file($file),
- ));
- return true;
- } else {
- return '文件资源不存在';
- }
- } catch (\Exception $e) {
- \Log::error('qcloud-cos上传文件流报错', $e->getMessage());
- return $e->getMessage();
- }
- }
- /**
- * 上传文件流
- * @param $bucket
- * @param $key
- * @param $srcPath
- * @param $cosClient
- */
- public function upload($key = '')
- {
- if (config('app.framework') == 'platform') {
- if (strexists($key, '/static/upload')) {
- $file = request()->getSchemeAndHttpHost() . $key;
- } else {
- $file = request()->getSchemeAndHttpHost() . '/static/upload/' . $key;
- }
- } else {
- $file = request()->getSchemeAndHttpHost() . '/attachment/' . $key;
- }
- if (!$file) {
- return false;
- }
- try {
- $this->cosClient->putObject(array(
- 'Bucket' => $this->bucket,
- 'Key' => $key,
- 'Body' => $this->curl_file($file),
- ));
- } catch (\Exception $e) {
- \Log::error('qcloud-cos上传文件流报错', $e->getMessage());
- return false;
- }
- return true;
- }
- public function delete($filename)
- {
- try {
- $result = $this->cosClient->deleteObject(array(
- 'Bucket' => $this->bucket,
- 'Key' => $filename,
- ));
- } catch (\Exception $e) {
- \Log::error('qcloud-cos删除文件失败', $e->getMessage());
- return false;
- }
- return true;
- }
- /**
- * 获取上传的url
- * @param $bucket
- * @param $key
- * @param $cosClient
- * @param $expire
- */
- public function getObjUrl($key = '')
- {
- $expire = 10;
- try {
- $signedUrl = $this->cosClient->getObjectUrl($this->bucket, $key, '+'.$expire.' minutes');
- // 请求成功
- return $signedUrl;
- } catch (\Exception $e) {
- // 请求失败
- \Log::error('qcloud-cos读取文件报错', $e);
- return $e->getMessage();
- }
- }
- public function curl_file($url)
- {
- $ch = curl_init();
- $timeout = 10;
- curl_setopt ($ch, CURLOPT_URL, $url);
- curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
- $file_contents = curl_exec($ch);
- curl_close($ch);
- return $file_contents;
- }
- }
|