<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2019/8/13
* Time: 15:23
*/
namespace app\controllers;
use Yii;
use yii\web\Controller;
use app\common\CommonController as Common;
use OSS\OssClient;
use OSS\Core\OssException;
use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;
class UploadController extends Controller{
public $enableCsrfValidation=false;
// public function actionIndex(){
// echo time();
// }
public function actionStore(){
$endpoint="oss-cn-beijing-internal.aliyuncs.com";
$get=Yii::$app->request->get();
$arr=[
'dishes'=>'store_img/Dishes/',
'group'=>'store_img/Group_img/',
'atlas'=>'store_img/Atlas/',
'license'=>'storeImg/license/',
'hetong'=>'storeImg/hetong/',
];
foreach ($arr as $key =>$val){
if($get['sign']==$key){
$path=$val;
}
}
AlibabaCloud::accessKeyClient('LTAIWDH', 'FhOIwHQcavzE')
->regionId('cn-hangzhou') // replace regionId as you need
->asDefaultClient();
try {
$result = AlibabaCloud::rpc()
->product('Sts')
->scheme('https') // https | http
->version('2015-04-01')
->action('AssumeRole')
->method('POST')
->options([
'query' => [
'RegionId' => "default",
'RoleArn' => "acs:ram::154",
'RoleSessionName' => "jfb-shfefefefef",
'DurationSeconds' => "3600",
],
])
->request();
$r=$result->toArray();
} catch (ClientException $e) {
echo $e->getErrorMessage() . PHP_EOL;
} catch (ServerException $e) {
echo $e->getErrorMessage() . PHP_EOL;
}
$arr=[
'tmp'=>$_FILES['file']['tmp_name'],
'size'=>$_FILES['file']['size'],
'name'=>$_FILES['file']['name'],
'type'=>$_FILES['file']['type']
];
$ext=explode(".",$arr['name']);
$fileName=date("Ymdhis").'--'.Common::getRandChar(10).'.'.$ext[1];
$object =$path.$fileName;
$address ='https://xxx.q11.com/'. $object;
$content = $arr['tmp'];
$ossClient = new OssClient($r['Credentials']['AccessKeyId'],$r['Credentials']['AccessKeySecret'],$endpoint, false,$r['Credentials']['SecurityToken']);
try{
$ossClient->uploadFile('jfb-shop', $object, $content);
$result=['code'=>200,'msg'=>['上传成功'],'url'=>$address];
echo json_encode($result);
}catch (OssException $e) {
print $e->getMessage();
}
}
}