当前位置:   article > 正文

Aws s3 (Java 使用亲测)_aws s3 java

aws s3 java

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<Boolean> 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<String> byAnalyzingId(@PathVariable("key") String key) {

        String url = awsS3Biz.downloadFile(key);
        return JsonResult.builder().data(url).build();
    }

    @GetMapping("/test")
    public JsonResult<String> 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依赖

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/285756
推荐阅读
相关标签
  

闽ICP备14008679号