现金大转盘 记录

阿里云oss文件上传
This commit is contained in:
2024-12-04 15:04:39 +08:00
parent 7e79d3fc35
commit c757bcf844
15 changed files with 457 additions and 2 deletions

View File

@@ -3,6 +3,7 @@ package com.sqx.modules.file;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.tea.TeaException;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.model.CannedAccessControlList;
import com.amazonaws.services.s3.model.ObjectMetadata;
@@ -27,6 +28,9 @@ import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import com.aliyun.sts20150401.models.AssumeRoleResponse;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
@@ -60,6 +64,44 @@ public class AliFileUploadController {
this.amazonS3 = amazonS3;
}
@GetMapping(value = "/getCredentials")
@ApiOperation("获取上传临时凭证")
public Result getCredentials() {
try {
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
// 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。
.setAccessKeyId("LTAI5tMmymNJatAoci3N5PVz")
// .setAccessKeyId(commonRepository.findOne(68).getValue())
// 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
// .setAccessKeySecret(commonRepository.findOne(69).getValue());
.setAccessKeySecret("WnJ2Dng7zxSagKj0NmhjfiHgIFyOEy");
// Endpoint 请参考 https://api.aliyun.com/product/Sts
config.endpoint = "sts.cn-shanghai.aliyuncs.com";
com.aliyun.sts20150401.Client client = new com.aliyun.sts20150401.Client(config);
com.aliyun.sts20150401.models.AssumeRoleRequest assumeRoleRequest = new com.aliyun.sts20150401.models.AssumeRoleRequest();
assumeRoleRequest.setRoleArn("acs:ram::1266409170752412:role/linshi");
assumeRoleRequest.setRoleSessionName("test");
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
// 复制代码运行请自行打印 API 的返回值
AssumeRoleResponse response = client.assumeRoleWithOptions(assumeRoleRequest, runtime);
return new Result().put("data",response.getBody().getCredentials());
} catch (TeaException error) {
return Result.error(error.getMessage());
// 诊断地址
// System.out.println(error.getData().get("Recommend"));
// com.aliyun.teautil.Common.assertAsString(error.message);
} catch (Exception _error) {
return Result.error(_error.getMessage());
// TeaException error = new TeaException(_error.getMessage(), _error);
// // 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
// // 诊断地址
// System.out.println(error.getData().get("Recommend"));
// com.aliyun.teautil.Common.assertAsString(error.message);
}
}
@RequestMapping(value = "/upload", method = RequestMethod.POST)
@ApiOperation("文件上传")
@ResponseBody