awsConfig
import com.amazonaws.ClientConfiguration;
import com.amazonaws.Protocol;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
@Slf4j
@Data
@EnableScheduling
@Configuration
public class AwsConfig {
@Value("${aws.accessKey}")
private String accessKey;
@Value("${aws.secretKey}")
private String secretKey;
@Value("${aws.region}")
private String region;
@Value("${aws.bucketName}")
private String bucketName;
@Bean
public AmazonS3 getAmazonS3() {
AWSCredentials awsCredentials = new BasicAWSCredentials(accessKey, secretKey);
ClientConfiguration baseOpts = new ClientConfiguration();
//
baseOpts.setSignerOverride("S3SignerType");
baseOpts.setProtocol(Protocol.HTTPS);
//
AmazonS3 amazonS3 = AmazonS3ClientBuilder.standard()
.withRegion(region)
.withCredentials(new AWSStaticCredentialsProvider(awsCredentials))
// .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(hostName, region)) // 如果有endpoint,可以用这个,这个和withRegion(Region)不能一起使用
// .withPathStyleAccessEnabled(true) // 如果配置了S3域名,就需要加这个进行路径访问,要不然会报AccessKey不存在的问题
.withClientConfiguration(baseOpts)
.build();
return amazonS3;
}
}
Biz层(也就是service层)
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.model.*;
import com.shulex.common.exception.BusinessException;
import com.shulex.voc.biz.VocAnalyzingBiz;
import com.shulex.voc.config.AwsConfig;
import com.shulex.voc.interceptor.UserContext;
import com.shulex.voc.model.VocAnalyzing;
import com.shulex.voc.service.VocAnalyzingService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;
@Slf4j
@Service
public class AwsS3Biz {
@Value("${aws.bucketName}")
private String bucketName;
@Autowired
private AwsConfig s3;
@Autowired
private VocAnalyzingService vocAnalyzingService;
public String up(MultipartFile multipartFile){
Objectmetadata objectmetadata = new Objectmetadata();
objectmetadata.setContentType(multipartFile.getContentType());
objectmetadata.setContentLength(multipartFile.getSize());
// 桶名,文件夹名,本地文件路径
String key = multipartFile.getOriginalFilename();
try {
s3.getAmazonS3().putObject(new PutObjectRequest(bucketName, key, multipartFile.getInputStream(), objectmetadata));
} catch (IOException e) {
e.printStackTrace();
}
return key;
}
public String upload(MultipartFile multipartFile,Long analyzingId) {
if (multipartFile.isEmpty()) {
throw new BusinessException(408,"文件为空!");
}
// 上传之前判断是否存在报表 getAwsKey //TODO getAwsKey
VocAnalyzing analyzing = new VocAnalyzing();
analyzing.setId(analyzingId);
analyzing.setAccountId(UserContext.get().getAccountId());
// if (!StringUtils.isEmpty(vocAnalyzingService.getOne(analyzingId).getAwsKey())){
// throw new BusinessException(406,"Manual report already exists.");
// }
//
try {
Objectmetadata objectmetadata = new Objectmetadata();
objectmetadata.setContentType(multipartFile.getContentType());
objectmetadata.setContentLength(multipartFile.getSize());
// 文件后缀
// String suffix = multipartFile.getOriginalFilename().substring(multipartFile.getOriginalFilename().lastIndexOf("."));
String key = UUID.randomUUID().toString();
PutObjectResult putObjectResult = s3.getAmazonS3()
.putObject(new PutObjectRequest(bucketName, key, multipartFile.getInputStream(), objectmetadata));
// 上传成功 关联到voc分析
if (null != putObjectResult) {
// 设置aws对象的 key
// analyzing.setAwsKey(key); //TODO
// vocAnalyzingService.update(analyzing);
// 返回key
return key;
}
} catch (Exception e) {
log.error("Upload files to the bucket,Failed:{}", e.getMessage());
e.printStackTrace();
}
return null;
}
public String downloadFile(String key) {
try {
if (StringUtils.isEmpty(key)) {
return null;
}
GeneratePresignedUrlRequest httpRequest = new GeneratePresignedUrlRequest(bucketName, key);
// //设置过期时间
// httpRequest.setExpiration(expirationDate);
return s3.getAmazonS3().generatePresignedUrl(httpRequest).toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public boolean isObjectExit(String bucketName, String key) {
int len = key.length();
ObjectListing objectListing = s3.getAmazonS3().listObjects(bucketName);
String s = new String();
for(S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) {
s = objectSummary.getKey();
int slen = s.length();
if(len == slen) {
int i;
for(i=0;i
controller层
import com.shulex.common.response.JsonResult;
import com.shulex.voc.biz.report.AwsS3Biz;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiOperationSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
@RestController
@RequestMapping(value = "/vocReport")
@Validated
@SuppressWarnings("unchecked")
@Api(value = "人工报告接口", tags = { "人工报告接口" })
public class VocReportController {
@Autowired
private AwsS3Biz awsS3Biz;
@ApiOperation(value = "导入人工报告",notes = "导入人工报告")
@ApiOperationSupport(order=0)
@PostMapping("/importReport")
public JsonResult importItems(@RequestParam("file") MultipartFile file, @RequestParam("analyzingId") Long analyzingId) throws IOException {
String key = awsS3Biz.upload(file,analyzingId);
return JsonResult.builder().data(key).build();
}
@ApiOperation(value = "根据key查看人工报告",notes = "根据key查看人工报告")
@ApiOperationSupport(order=1)
@GetMapping("/{key}")
public JsonResult byAnalyzingId(@PathVariable("key") String key) {
String url = awsS3Biz.downloadFile(key);
return JsonResult.builder().data(url).build();
}
@GetMapping("/test")
public JsonResult test() {
awsS3Biz.getAllBucketObject();
return JsonResult.builder().data("ok").build();
}
}
yml文件
aws:
region: x
accessKey: x
secretKey: xxxx
bucketName: xxx
pom依赖
com.amazonaws
aws-java-sdk-s3
1.11.821



