前端进行阿里云Oss图片上传前,STStoken的获取

前端小伙伴进行Oss操作时需要调用接口获得凭证,以下可以直接复用:

 

package com.szlaozi.ldd.company.controller;

import com.aliyun.oss.ClientException;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.auth.sts.AssumeRoleRequest;
import com.aliyuncs.auth.sts.AssumeRoleResponse;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.szlaozi.ldd.company.controller.entity.StsToken;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author:   Liu...cx..
 * @Description:
 * @Date: 2020/1/18 9:55
 * @Modified:
 */
@Slf4j
@RestController
@CrossOrigin
public class CommonController {


    @Value("${aliyun.sts.accessKeyId}")
    private String accesskeyid;
    @Value("${aliyun.sts.accessKeySecret}")
    private String accesskeysecret;
    @Value("${aliyun.sts.endPoint}")
    private String endPoint;
    @Value("${aliyun.sts.roleArn}")
    private String rolearn;
    @Value("${aliyun.sts.roleSessionName}")
    private String rolesessionName;

    /**
     * 获取阿里云图片上传 STStoken接口
     *
     * @return
     */
    @PostMapping("/companies/getStsToken")
    public StsToken getStsToken() {
        String endpoint = endPoint;
        String accessKeyId = accesskeyid;
        String accessKeySecret = accesskeysecret;
        String roleArn = rolearn;
        String roleSessionName = rolesessionName;
        try {
            // 添加endpoint(直接使用STS endpoint,前两个参数留空,无需添加region ID)
            DefaultProfile.addEndpoint("", "", "Sts", endpoint);
            // 构造default profile(参数留空,无需添加region ID)
            IClientProfile profile = DefaultProfile.getProfile("", accessKeyId, accessKeySecret);
            // 用profile构造client
            DefaultAcsClient client = new DefaultAcsClient(profile);
            final AssumeRoleRequest request = new AssumeRoleRequest();
            request.setMethod(MethodType.POST);
            request.setRoleArn(roleArn);
            request.setRoleSessionName(roleSessionName);
            request.setDurationSeconds(1000L); // 设置凭证有效时间
            final AssumeRoleResponse response = client.getAcsResponse(request);
            log.info("->Expiration is -> {}:", response.getCredentials().getExpiration() + ",Access Key Id:" + response.getCredentials().getAccessKeyId() + ",Access Key Secret:" + response.getCredentials().getAccessKeySecret() + ",Security Token:" + response.getCredentials().getSecurityToken() + ",RequestId:" + response.getRequestId());
            StsToken stsToken = new StsToken();
            stsToken.setAccessKeyId(response.getCredentials().getAccessKeyId());
            stsToken.setAccessKeySecret(response.getCredentials().getAccessKeySecret());
            stsToken.setStsToken(response.getCredentials().getSecurityToken());
            return stsToken;
        } catch (ClientException e) {
            e.printStackTrace();
            log.info("->Error code: is -> {}:", e.getErrorCode() + ",Error message:" + e.getErrorMessage() + ",RequestId:" + e.getRequestId());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (com.aliyuncs.exceptions.ClientException e) {
            e.printStackTrace();
        }
        return null;
    }
}
 

其中需要配置文件:

   sts:
       accessKeyId:  *************************
       accessKeySecret: *************************
       endPoint: *************************
       roleArn: acs:ram: *************************
       roleSessionName:  *************************

相关依赖:

        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-sts</artifactId>
            <version>3.0.0</version>
        </dependency>
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-core</artifactId>
            <version>3.5.0</version>
        </dependency>

 

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值