获取临时上传凭证

This commit is contained in:
wangw 2024-09-05 17:32:00 +08:00
parent 72a3057126
commit 81b6454557
4 changed files with 61 additions and 15 deletions

View File

@ -39,6 +39,11 @@
<version>2.6</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>sts20150401</artifactId>
<version>1.1.4</version>
</dependency>
<!--支付宝依赖-->
<dependency>
<groupId>com.alipay.sdk</groupId>

View File

@ -20,8 +20,11 @@ import cn.ysk.cashier.annotation.rest.AnonymousGetMapping;
import cn.ysk.cashier.annotation.rest.AnonymousPostMapping;
import cn.ysk.cashier.domain.QiniuConfig;
import cn.ysk.cashier.domain.QiniuContent;
import cn.ysk.cashier.exception.BadRequestException;
import cn.ysk.cashier.service.QiNiuService;
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.ApiOperation;
import lombok.RequiredArgsConstructor;
@ -98,6 +101,36 @@ public class QiniuController {
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("上传文件")
@ApiOperation("上传文件")

View File

@ -21,6 +21,7 @@ import cn.ysk.cashier.service.dto.QiniuQueryCriteria;
import com.dianguang.cloud.ossservice.config.CloudStorageConfig;
import org.springframework.data.domain.Pageable;
import org.springframework.web.multipart.MultipartFile;
import com.aliyun.teaopenapi.models.Config;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@ -41,6 +42,8 @@ public interface QiNiuService {
CloudStorageConfig findCloud();
Config findTempCloud();
/**
* 修改配置
* @param qiniuConfig 配置

View File

@ -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;
import cn.hutool.core.util.ObjectUtil;
import cn.ysk.cashier.domain.QiniuConfig;
import cn.ysk.cashier.exception.BadRequestException;
import com.aliyun.teaopenapi.models.Config;
import com.dianguang.cloud.ossservice.config.CloudStorageConfig;
import com.dianguang.cloud.ossservice.service.OSSFactory;
import com.qiniu.common.QiniuException;
@ -93,6 +79,25 @@ public class QiNiuServiceImpl implements QiNiuService {
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
@CachePut(key = "'config'")
@Transactional(rollbackFor = Exception.class)