Aws s3 (Java 使用)

20 篇文章 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;
    }
}

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());
    }


}

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();
    }
}

yml文件

aws:
  region: x
  accessKey: x
  secretKey: xxxx
  bucketName: xxx

pom依赖

<dependency>
     <groupId>com.amazonaws</groupId>
     <artifactId>aws-java-sdk-s3</artifactId>
     <version>1.11.821</version>
</dependency>
  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
AWS S3 Java SDK V2 中,可以通过实现 `software.amazon.awssdk.core.sync.ResponseTransformer` 接口并重写其中的 `onResponse()` 和 `exceptionOccurred()` 方法来实现 TransferListener。在 `onResponse()` 方法中,您可以获得已经传输的字节数和需要传输的总字节数,并且可以通过它们来计算传输的进度。 以下是一个示例代码片段,展示了如何实现 TransferListener: ``` public class S3TransferListener implements ResponseTransformer<GetObjectResponse, GetObjectResponse> { private final long totalBytes; private final TransferListener listener; private long transferredBytes = 0; public S3TransferListener(long totalBytes, TransferListener listener) { this.totalBytes = totalBytes; this.listener = listener; } @Override public GetObjectResponse transformResponse(GetObjectResponse response) { // Do something with the response return response; } @Override public void onResponse(GetObjectResponse response) { try (InputStream contentStream = response.responseBody()) { byte[] buffer = new byte[8192]; int bytesRead; while ((bytesRead = contentStream.read(buffer)) != -1) { // Update the transferred bytes transferredBytes += bytesRead; // Calculate the progress percentage int progress = (int) ((double) transferredBytes / totalBytes * 100); // Notify the listener of the progress update listener.progressChanged(ProgressEvent.builder() .bytesTransferred(transferredBytes) .totalBytes(totalBytes) .percentTransfered(progress) .build()); } } catch (IOException e) { throw new RuntimeException(e); } } @Override public void exceptionOccurred(Throwable error) { // Handle the exception } } ``` 在上面的代码中,`transferredBytes` 变量跟踪已经传输的字节数,`totalBytes` 变量是需要传输的总字节数。在 `onResponse()` 方法中,我们迭代读取传输的字节,并更新 `transferredBytes` 变量,计算进度百分比,最后通过 `listener.progressChanged()` 方法通知进度变化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值