dever 4 年之前
父节点
当前提交
b70c3ff0e6
共有 5 个文件被更改,包括 79 次插入44 次删除
  1. 2 2
      database/yun.php
  2. 1 1
      lib/Store/Oss.php
  3. 58 34
      lib/View/Oss.php
  4. 17 7
      lib/View/Qiniu.php
  5. 1 0
      src/Yun.php

+ 2 - 2
database/yun.php

@@ -120,9 +120,9 @@ return array
             'match'     => 'option',
         ),
 
-        'token_time'      => array
+        'token_endtime'      => array
         (
-            'type'      => 'varchar-800',
+            'type'      => 'int-11',
             'name'      => 'token失效时间',
             'default'   => '',
             'desc'      => 'token',

+ 1 - 1
lib/Store/Oss.php

@@ -18,7 +18,7 @@ class Oss extends Core implements Config
         $config = Dever::db('upload/yun')->one($this->config['yun']);
         $accessKey = $config['appkey'];
         $secretKey = $config['appsecret'];
-        $endpoint = "http://oss-cn-beijing.aliyuncs.com";
+        $endpoint = "http://oss-cn-".$config['region_id'].".aliyuncs.com";
         $bucket = $this->config['bucket'];
         $this->host = $config['host'];
         $ossClient = new OssClient($accessKey, $secretKey, $endpoint);

+ 58 - 34
lib/View/Oss.php

@@ -11,45 +11,69 @@ use AlibabaCloud\Client\Exception\ClientException;
 use AlibabaCloud\Client\Exception\ServerException;
 class Oss
 {
-	public function token($config, $upload)
+    public function token($config, $upload)
     {
-    	//构建一个阿里云客户端,用于发起请求。
-		//构建阿里云客户端时需要设置AccessKey ID和AccessKey Secret。
-		AlibabaCloud::accessKeyClient($config['appkey'], $config['appsecret'])->regionId('cn-beijing')->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' => "cn-beijing",
-                              'RoleArn' => "acs:ram::1118875946432366:role/api",
-                              'RoleSessionName' => "upload",
-                            ],
-                        ])
-              ->request();
-		    print_r($result->toArray());die;
-		} catch (ClientException $e) {
-		    echo $e->getErrorMessage() . PHP_EOL;die;
-		} catch (ServerException $e) {
-		    echo $e->getErrorMessage() . PHP_EOL;die;
-		}
-        return array('oss', '', '');
+        $region = 'cn-' . $config['region_id'];
+        if (!$config['role_arn']) {
+            return array('oss', '||' . $config['appkey'] . '||' . $config['appsecret'], $region);
+        }
+        $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);
     }
 
     public function callback()
-	{
-		$body = file_get_contents('php://input');
-		Dever::log($body, 'oss_callback');
-		$body = json_decode($body, true);
+  	{
+  		$body = file_get_contents('php://input');
+  		Dever::log($body, 'oss_callback');
+  		$body = json_decode($body, true);
 
-		return $body;
-	}
+  		return $body;
+  	}
 
 	# 视频转码
     public function convert($key, $file, $config, $upload)

+ 17 - 7
lib/View/Qiniu.php

@@ -8,15 +8,25 @@ class Qiniu
 {
 	public function token($config, $upload)
     {
-        $auth = new \Qiniu\Auth($config['appkey'], $config['appsecret']);
+        $cur = time();
+        if (!$config['token'] || ($config['token_endtime'] && $cur > $config['token_endtime'])) {
+            $auth = new \Qiniu\Auth($config['appkey'], $config['appsecret']);
 
-        # 暂时没用到callback
-        $policy = array(
-            'callbackUrl' => Dever::url('yun.callback?config=' . $config['id'], 'upload'),
-            'callbackBody' => 'filename=$(fname)&key=$(key)&filesize=$(fsize)&width=$(imageInfo.width)&height=$(imageInfo.height)'
-        );
+            # 暂时没用到callback
+            $policy = array(
+                'callbackUrl' => Dever::url('yun.callback?config=' . $config['id'], 'upload'),
+                'callbackBody' => 'filename=$(fname)&key=$(key)&filesize=$(fsize)&width=$(imageInfo.width)&height=$(imageInfo.height)'
+            );
 
-        $token = $auth->uploadToken($upload['bucket'], null, 3600);
+            $token = $auth->uploadToken($upload['bucket'], null, 3600);
+            $up['token'] = $token;
+            $up['token_endtime'] = $time + 3600 - 60;
+            $up['where_id'] = $config['id'];
+            Dever::db('upload/yun')->update($up);
+        } else {
+            $token = $config['token'];
+        }
+        
         $domain = 'http://up-z1.qiniup.com/';
 
         return array('qiniu', $token, $domain);

+ 1 - 0
src/Yun.php

@@ -39,6 +39,7 @@ class Yun
         
         list($type, $token, $domain) = $this->getMethod($config['type'])->token($config, $upload);
         $result = array('type' => $type, 'uptoken' => $token, 'domain' => $domain, 'host' => $config['host']);
+        print_r($result);die;
         if ($return == 1) {
             return $result;
         }