阿里oss+sts

<?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();

}

}

 

}

转载于:https://my.oschina.net/u/3371661/blog/3094925

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值