获取临时上传凭证
This commit is contained in:
@@ -39,6 +39,11 @@
|
|||||||
<version>2.6</version>
|
<version>2.6</version>
|
||||||
<scope>compile</scope>
|
<scope>compile</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.aliyun</groupId>
|
||||||
|
<artifactId>sts20150401</artifactId>
|
||||||
|
<version>1.1.4</version>
|
||||||
|
</dependency>
|
||||||
<!--支付宝依赖-->
|
<!--支付宝依赖-->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.alipay.sdk</groupId>
|
<groupId>com.alipay.sdk</groupId>
|
||||||
|
|||||||
@@ -20,8 +20,11 @@ import cn.ysk.cashier.annotation.rest.AnonymousGetMapping;
|
|||||||
import cn.ysk.cashier.annotation.rest.AnonymousPostMapping;
|
import cn.ysk.cashier.annotation.rest.AnonymousPostMapping;
|
||||||
import cn.ysk.cashier.domain.QiniuConfig;
|
import cn.ysk.cashier.domain.QiniuConfig;
|
||||||
import cn.ysk.cashier.domain.QiniuContent;
|
import cn.ysk.cashier.domain.QiniuContent;
|
||||||
|
import cn.ysk.cashier.exception.BadRequestException;
|
||||||
import cn.ysk.cashier.service.QiNiuService;
|
import cn.ysk.cashier.service.QiNiuService;
|
||||||
import cn.ysk.cashier.service.dto.QiniuQueryCriteria;
|
import cn.ysk.cashier.service.dto.QiniuQueryCriteria;
|
||||||
|
import com.aliyun.sts20150401.models.AssumeRoleResponse;
|
||||||
|
import com.aliyun.tea.TeaException;
|
||||||
import io.swagger.annotations.Api;
|
import io.swagger.annotations.Api;
|
||||||
import io.swagger.annotations.ApiOperation;
|
import io.swagger.annotations.ApiOperation;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
@@ -98,6 +101,36 @@ public class QiniuController {
|
|||||||
return new ResponseEntity<>(map,HttpStatus.OK);
|
return new ResponseEntity<>(map,HttpStatus.OK);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@AnonymousGetMapping(value = "/getCredentials")
|
||||||
|
@ApiOperation("获取上传临时凭证")
|
||||||
|
public ResponseEntity<Object> getCredentials() {
|
||||||
|
try {
|
||||||
|
com.aliyun.sts20150401.Client client = new com.aliyun.sts20150401.Client(qiNiuService.findTempCloud());
|
||||||
|
com.aliyun.sts20150401.models.AssumeRoleRequest assumeRoleRequest = new com.aliyun.sts20150401.models.AssumeRoleRequest();
|
||||||
|
assumeRoleRequest.setRoleArn("acs:ram::1413456038175003:role/oss");
|
||||||
|
assumeRoleRequest.setRoleSessionName("test");
|
||||||
|
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
|
||||||
|
// 复制代码运行请自行打印 API 的返回值
|
||||||
|
AssumeRoleResponse response = client.assumeRoleWithOptions(assumeRoleRequest, runtime);
|
||||||
|
return new ResponseEntity<>(response.getBody().getCredentials(),HttpStatus.OK);
|
||||||
|
} catch (TeaException error) {
|
||||||
|
throw new BadRequestException("获取失败,请联系管理员。"+error.getMessage());
|
||||||
|
// System.out.println(error.getMessage());
|
||||||
|
// 诊断地址
|
||||||
|
// System.out.println(error.getData().get("Recommend"));
|
||||||
|
// com.aliyun.teautil.Common.assertAsString(error.message);
|
||||||
|
} catch (Exception _error) {
|
||||||
|
throw new BadRequestException("获取失败,请联系管理员。"+ _error.getMessage());
|
||||||
|
// TeaException error = new TeaException(_error.getMessage(), _error);
|
||||||
|
// // 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
|
||||||
|
// // 错误 message
|
||||||
|
// System.out.println(error.getMessage());
|
||||||
|
// // 诊断地址
|
||||||
|
// System.out.println(error.getData().get("Recommend"));
|
||||||
|
// com.aliyun.teautil.Common.assertAsString(error.message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// @Log("上传文件")
|
// @Log("上传文件")
|
||||||
@ApiOperation("上传文件")
|
@ApiOperation("上传文件")
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ import cn.ysk.cashier.service.dto.QiniuQueryCriteria;
|
|||||||
import com.dianguang.cloud.ossservice.config.CloudStorageConfig;
|
import com.dianguang.cloud.ossservice.config.CloudStorageConfig;
|
||||||
import org.springframework.data.domain.Pageable;
|
import org.springframework.data.domain.Pageable;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
import com.aliyun.teaopenapi.models.Config;
|
||||||
|
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@@ -41,6 +42,8 @@ public interface QiNiuService {
|
|||||||
|
|
||||||
CloudStorageConfig findCloud();
|
CloudStorageConfig findCloud();
|
||||||
|
|
||||||
|
Config findTempCloud();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 修改配置
|
* 修改配置
|
||||||
* @param qiniuConfig 配置
|
* @param qiniuConfig 配置
|
||||||
|
|||||||
@@ -1,23 +1,9 @@
|
|||||||
/*
|
|
||||||
* Copyright 2019-2020 Zheng Jie
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
package cn.ysk.cashier.service.impl;
|
package cn.ysk.cashier.service.impl;
|
||||||
|
|
||||||
import cn.hutool.core.util.ObjectUtil;
|
import cn.hutool.core.util.ObjectUtil;
|
||||||
import cn.ysk.cashier.domain.QiniuConfig;
|
import cn.ysk.cashier.domain.QiniuConfig;
|
||||||
import cn.ysk.cashier.exception.BadRequestException;
|
import cn.ysk.cashier.exception.BadRequestException;
|
||||||
|
import com.aliyun.teaopenapi.models.Config;
|
||||||
import com.dianguang.cloud.ossservice.config.CloudStorageConfig;
|
import com.dianguang.cloud.ossservice.config.CloudStorageConfig;
|
||||||
import com.dianguang.cloud.ossservice.service.OSSFactory;
|
import com.dianguang.cloud.ossservice.service.OSSFactory;
|
||||||
import com.qiniu.common.QiniuException;
|
import com.qiniu.common.QiniuException;
|
||||||
@@ -93,6 +79,25 @@ public class QiNiuServiceImpl implements QiNiuService {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Config findTempCloud() {
|
||||||
|
Optional<QiniuConfig> qiniuConfig = qiNiuConfigRepository.findById(1L);
|
||||||
|
QiniuConfig config= qiniuConfig.orElseGet(QiniuConfig::new);
|
||||||
|
if(ObjectUtil.isNotEmpty(config)){
|
||||||
|
com.aliyun.teaopenapi.models.Config config1 = new com.aliyun.teaopenapi.models.Config()
|
||||||
|
// 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。
|
||||||
|
// .setAccessKeyId(System.getenv("LTAI5tPdEfYSZcqHbjCrtPRD"))
|
||||||
|
.setAccessKeyId(qiniuConfig.get().getAccessKey())
|
||||||
|
// 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
|
||||||
|
.setAccessKeySecret(qiniuConfig.get().getSecretKey());
|
||||||
|
// Endpoint 请参考 https://api.aliyun.com/product/Sts
|
||||||
|
config1.endpoint = "sts.cn-hangzhou.aliyuncs.com";
|
||||||
|
return config1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@CachePut(key = "'config'")
|
@CachePut(key = "'config'")
|
||||||
@Transactional(rollbackFor = Exception.class)
|
@Transactional(rollbackFor = Exception.class)
|
||||||
|
|||||||
Reference in New Issue
Block a user