Aws s3 实现文件上传和下载 (Java 使用)

颛孙霖
2023-12-01

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 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;




    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,"文件为空!");
        }
        
        //
        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) {

                // 返回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 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;

/**
 * @author 三十七度
 */
@RestController
@RequestMapping(value = "/test")
@Validated
@SuppressWarnings("unchecked")
@Api(value = "AWS接口", tags = { "AWS接口" })
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>

 类似资料: