Aws s3 (Java 使用亲测)

待我称王封你为后i 2023-09-27 00:59 234阅读 0赞

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;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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;
}

/**
* 判断名为bucketName的bucket里面是否有一个名为key的object
* @param bucketName
* @param key
* @return
*/
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<len;i++) {
if(s.charAt(i) != key.charAt(i)) {
break;
}
}
if(i == len) {
return true;
}
}
}
return false;
}

public void getAllBucketObject(){
ObjectListing objects = s3.getAmazonS3().listObjects(bucketName);
do {
for (S3ObjectSummary objectSummary : objects.getObjectSummaries()) {
System.out.println(“Object: “ + objectSummary.getKey());
}
objects = s3.getAmazonS3().listNextBatchOfObjects(objects);
} while (objects.isTruncated());
}

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
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;

/**
* Web控制层:
* @author liaoguang
* @date 2022-03-14
*/
@RestController
@RequestMapping(value = “/vocReport”)
@Validated
@SuppressWarnings(“unchecked”)
@Api(value = “人工报告接口”, tags = { “人工报告接口” })
public class VocReportController {

@Autowired
private AwsS3Biz awsS3Biz;

/**
* 导入人工报告
* @param file
* @param analyzingId
* @return
* @throws IOException
*/
@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();
}

/**
* 查看人工报告
* @param
* @return
*/
@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();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
yml文件

aws:
region: x
accessKey: x
secretKey: xxxx
bucketName: xxx
1
2
3
4
5
pom依赖

com.amazonaws

aws-java-sdk-s3

1.11.821

————————————————
版权声明:本文为CSDN博主「普通网友」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/web15085599741/article/details/123595974

发表评论

表情:
评论列表 (有 0 条评论,234人围观)

还没有评论,来说两句吧...

相关阅读

    相关 AWS S3文件/文件夹删除

    1. 关于AWS S3的使用相关知识总结可以参考:[AWS S3 学习小结][AWS S3]。 2. 以下主要是想总结一下关于S3文件、文件夹如何删除,示例版本是源码里面的j

    相关 AWS CLI使用s3

    aws CLI是什么东西,暂且先不去了解,目前的需求是s3. 我在Jenkins上创建一个bucket,然后申请access\_key,然后就可以使用s3来存储数据了。也就是

    相关 AWS S3 学习小结

    1.首先,这个是AWS的开发资源使用文档:[AWS开发文档][AWS],[AWS官网 - S3教程][AWS_ - S3] 2.我们可以通过AWS Cli和Java Api来