diff --git a/pom.xml b/pom.xml index fb14fda..8a8dee9 100644 --- a/pom.xml +++ b/pom.xml @@ -231,6 +231,12 @@ ehcache + + com.aliyun.oss + aliyun-sdk-oss + 3.10.2 + + diff --git a/src/main/java/com/subsidy/controller/OssController.java b/src/main/java/com/subsidy/controller/OssController.java new file mode 100644 index 0000000..0a1b529 --- /dev/null +++ b/src/main/java/com/subsidy/controller/OssController.java @@ -0,0 +1,79 @@ +package com.subsidy.controller; + +import com.aliyun.oss.OSS; +import com.aliyun.oss.OSSClientBuilder; +import com.aliyun.oss.model.PutObjectRequest; +import com.aliyun.oss.model.PutObjectResult; +import com.subsidy.common.ResponseData; +import com.subsidy.common.ResponseVO; +import com.subsidy.vo.oss.GetSecretVO; +import com.subsidy.vo.oss.UploadFileVO; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.multipart.MultipartFile; + +@Api(tags = "oss") +@RestController +@RequestMapping("/oss") +public class OssController { + + public static final String keyId = "LTAI5tPAH7P7WQVeowo517BE"; + + + public static final String secret = "0ueqhIfdAZyw5lWlBVSLpAxTtx37RY"; + + + + @Value("${spring.profiles.active}") + private String env; + + @PostMapping("getSecret") + @ApiOperation("oss秘钥") + public ResponseVO getSecret(){ + GetSecretVO getSecretVO = new GetSecretVO(); + getSecretVO.setKey(keyId); + getSecretVO.setSecret(secret); + return ResponseData.generateCreatedResponse(0,getSecretVO); + } + + @PostMapping("uploadFile") + @ApiOperation("上传文件 file") + public ResponseVO uploadFile(@RequestParam("file") MultipartFile file){ + // 创建OSSClient实例。 + OSS ossClient = new OSSClientBuilder().build("http://oss-cn-shanghai.aliyuncs.com", keyId, secret); + UploadFileVO uploadFileVO = new UploadFileVO(); + try { + + if (env.equals("test")){ + env = "shixi-ai-test"; + }else { + env = "shixi-ai"; + } + + + // 创建PutObjectRequest对象。 + PutObjectRequest putObjectRequest = new PutObjectRequest(env, System.currentTimeMillis()+"_"+file.getOriginalFilename(), file.getInputStream()); + // 设置该属性可以返回response。如果不设置,则返回的response为空。 + putObjectRequest.setProcess("true"); + // 创建PutObject请求。 + PutObjectResult result = ossClient.putObject(putObjectRequest); + // 如果上传成功,则返回200。 +// System.out.println(result.getResponse().getUri()); + uploadFileVO.setUrl(result.getResponse().getUri()); + } catch (Exception oe) { + oe.printStackTrace(); + } finally { + if (ossClient != null) { + ossClient.shutdown(); + } + } + return ResponseData.generateCreatedResponse(0,uploadFileVO); + } + + +} diff --git a/src/main/java/com/subsidy/vo/oss/GetSecretVO.java b/src/main/java/com/subsidy/vo/oss/GetSecretVO.java new file mode 100644 index 0000000..046b364 --- /dev/null +++ b/src/main/java/com/subsidy/vo/oss/GetSecretVO.java @@ -0,0 +1,13 @@ +package com.subsidy.vo.oss; + +import lombok.Data; + +@Data +public class GetSecretVO { + + private String key; + + private String secret; + + +} diff --git a/src/main/java/com/subsidy/vo/oss/UploadFileVO.java b/src/main/java/com/subsidy/vo/oss/UploadFileVO.java new file mode 100644 index 0000000..28cb3fa --- /dev/null +++ b/src/main/java/com/subsidy/vo/oss/UploadFileVO.java @@ -0,0 +1,10 @@ +package com.subsidy.vo.oss; + +import lombok.Data; + +@Data +public class UploadFileVO { + + private String url; + +}