| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 | 
							- <?php
 
- namespace Upload\Lib\View;
 
- use Dever;
 
- Dever::apply('sdk/oss', 'upload');
 
- Dever::apply('vendor/autoload', 'alibaba');
 
- use OSS\OssClient;
 
- use OSS\Core\OssException;
 
- use AlibabaCloud\Client\AlibabaCloud;
 
- use AlibabaCloud\Client\Exception\ClientException;
 
- use AlibabaCloud\Client\Exception\ServerException;
 
- class Oss
 
- {
 
-     private $client;
 
-     public function token($config, $upload, $file = '')
 
-     {
 
-         $region = 'cn-' . $config['region_id'];
 
-         if (!$config['role_arn']) {
 
-             return array('oss', '||' . $config['appkey'] . '||' . $config['appsecret'], $region, $upload['bucket']);
 
-         }
 
-         $cur = time();
 
-         
 
-         if (!$config['token'] || ($config['token_endtime'] && $cur > $config['token_endtime'])) {
 
-             AlibabaCloud::accessKeyClient($config['appkey'], $config['appsecret'])->regionId($region)->asDefaultClient();
 
-             //设置参数,发起请求。
 
-             try {
 
-                 $result = AlibabaCloud::rpc()
 
-                 ->product('Sts')
 
-                 ->scheme('https') // https | http
 
-                 ->version('2015-04-01')
 
-                 ->action('AssumeRole')
 
-                 ->method('POST')
 
-                 ->host('sts.aliyuncs.com')
 
-                 ->options([
 
-                     'query' => [
 
-                       'RegionId' => $region,
 
-                       'RoleArn' => "acs:ram::1118875946432366:role/api",
 
-                       'RoleArn' => $config['role_arn'],
 
-                       'RoleSessionName' => "upload",
 
-                     ],
 
-                 ])
 
-                 ->request();
 
-                 $data = $result->toArray();
 
-                 if (isset($data['Credentials']['SecurityToken'])) {
 
-                     $token = $data['Credentials']['SecurityToken'];
 
-                     $endtime = $data['Credentials']['Expiration'];
 
-                     $appkey = $data['Credentials']['AccessKeyId'];
 
-                     $appsecret = $data['Credentials']['AccessKeySecret'];
 
-                     $token = $token . '||' . $appkey . '||' . $appsecret;
 
-                     $up['token'] = $token;
 
-                     $up['token_endtime'] = Dever::maketime($endtime) - 60;
 
-                     $up['where_id'] = $config['id'];
 
-                     Dever::db('upload/yun')->update($up);
 
-                 } else {
 
-                     echo 'oss token获取失败,请检查配置';die;
 
-                 }
 
-             } catch (ClientException $e) {
 
-                 echo $e->getErrorMessage() . PHP_EOL;die;
 
-             } catch (ServerException $e) {
 
-                 echo $e->getErrorMessage() . PHP_EOL;die;
 
-             }
 
-         } else {
 
-             $token = $config['token'];
 
-         }
 
-         
 
-         return array('oss', $token, $region, $upload['bucket']);
 
-     }
 
-     # 连接
 
-     public function connect($config, $upload, $file = '')
 
-     {
 
-         $accessKey = $config['appkey'];
 
-         $secretKey = $config['appsecret'];
 
-         $endpoint = "http://oss-cn-".$config['region_id'].".aliyuncs.com";
 
-         list($type, $token, $domain, $bucket) = $this->token($config, $upload, $file);
 
-         $data = explode('||', $token);
 
-         if ($data[0]) {
 
-             $token = $data[0];
 
-             $accessKey = $data[1];
 
-             $secretKey = $data[2];
 
-         } else {
 
-             $token = false;
 
-         }
 
-         $this->client = new OssClient($accessKey, $secretKey, $endpoint, false, $token);
 
-         return $this;
 
-     }
 
-     # 上传文件
 
-     public function upload($bucket, $file, $source_file, $options = array(), $base64 = false)
 
-     {
 
-         if (!$this->client) {
 
-             return array();
 
-         }
 
-         $method = 'uploadFile';
 
-         if ($base64) {
 
-             $method = 'putObject';
 
-         }
 
-         $ret = $this->client->$method($bucket, $file, $source_file, $options);
 
-         return $ret;
 
-     }
 
-     # 删除文件
 
-     public function delete($bucket, $file)
 
-     {
 
-         $this->client = new \Qiniu\Storage\BucketManager($this->auth);
 
-         $result = $this->client->delete($bucket, $file);
 
-         return $result;
 
-     }
 
-     # 下载文件
 
-     public function download($bucket, $file, $local = false)
 
-     {
 
-         if (!$this->client) {
 
-             return false;
 
-         }
 
-         if ($local) {
 
-             $options = array(
 
-                 OssClient::OSS_FILE_DOWNLOAD => $local
 
-             );
 
-         } else {
 
-             $options = array();
 
-         }
 
-         
 
-         $content = $this->client->getObject($bucket, $file, $options);
 
-         return $content;
 
-     }
 
-     public function callback()
 
-   	{
 
-   		$body = file_get_contents('php://input');
 
-   		Dever::log($body, 'oss_callback');
 
-   		$body = json_decode($body, true);
 
-   		return $body;
 
-   	}
 
- 	# 视频转码
 
-     public function convert($key, $file, $config, $upload)
 
-     {
 
-         //?x-oss-process=video/snapshot,t_7000,f_jpg,w_800,h_600,m_fast
 
-         //t以后是毫秒
 
-     }
 
-     # 视频截图
 
-     public function cover($key, $video, $num = 1)
 
-     {
 
-         $num = $num * 1000;
 
-         $file = $video . '?x-oss-process=video/snapshot,t_'.$num.',f_jpg,m_fast';
 
-         if ($local == 1) {
 
-             $data = Dever::load('upload/save')->copy($file, 7, false, false, 'jpg');
 
-             return $data['url'] . '?time=' . time();
 
-         } else {
 
-             return $file;
 
-         }
 
-         //x-oss-process=video/snapshot,t_1000,f_jpg,m_fast 
 
-     }
 
- }
 
 
  |