Bootstrap

亚马逊S3的使用

一、下载s3browser客户端

1、进入下载页面进行下载

下载亚马逊S3客户端地址

2、使用客户端进行连接公司亚马逊服务

 连接成功之后的界面如下:

可以一目了然的看到上传之后的文件和文件目录存储结构。

二、亚马逊 AmazonS3的使用

1、引入pom依赖

<dependency>
    <groupId>com.amazonaws</groupId>
    <artifactId>aws-java-sdk-s3</artifactId>
    <version>1.11.792</version>
</dependency>

 2、application.properties添加配置

#-------------------- S3 start -------------------
# http or https
amazonaws.s3.protocol = http
# host:port       (test1.endpoint=192.168.66.224:7480)
amazonaws.s3.endpoint = 192.168.66.224:7480
# accessKey    (test1.accessKey=123456)
amazonaws.s3.accessKey = 123456
# secretKey    (test1.secretKey=123456)
amazonaws.s3.secretKey = 123456
# maxConnections   (def=50)
amazonaws.s3.maxConnections = 50
# connectionTimeout   (def=10000)
amazonaws.s3.connectionTimeout = 10000
# socketTimeout   (def=50000)
amazonaws.s3.socketTimeout = 50000
# useGzip   (def=false)
amazonaws.s3.useGzip = false
# bucketName   (def=icollect)
amazonaws.s3.bucketName = icollect
#amazonaws.s3.template.key (def=template.xlsx)
amazonaws.s3.templateKey = template
#amazonaws.s3.template.path (def=template.xlsx)
amazonaws.s3.templatePath = D://template
#-------------------- S3 end -------------------

3 、添加配置类AmazonS3Properties

import com.amazonaws.ClientConfiguration;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

/**
 * @author 痞子磊
 */
@NoArgsConstructor
@AllArgsConstructor
@Data
@ConfigurationProperties(prefix = "amazonaws.s3")
@Configuration
public class AmazonS3Properties {
    //private String regionName;
    private String endpoint;
    private String accessKey;
    private String secretKey;
    private String protocol = "http";
    private int maxConnections = ClientConfiguration.DEFAULT_MAX_CONNECTIONS;
    private int connectionTimeout = ClientConfiguration.DEFAULT_CONNECTION_TIMEOUT;
    private int socketTimeout = ClientConfiguration.DEFAULT_SOCKET_TIMEOUT;
    private boolean useGzip = ClientConfiguration.DEFAULT_USE_GZIP;
}

4、添加Bean初始化AmazonS3AutoConfiguration

import com.amazonaws.ClientConfiguration;
import com.amazonaws.Protocol;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author chenxiaolei
 */
//加这个注解表示为配置类
@Configuration
//能够配置Properties配置类
@EnableConfigurationProperties(AmazonS3Properties.class)
//因为在aMapper1上面标识了AMapper类型的bean只能有一个实现 @ConditionalOnMissingBean(AMapper.class),所以在进行aMapper2注册时,系统会出现上面图上的异常,这是正常的。
//当我们把 @ConditionalOnMissingBean(AMapper.class) 去掉之后,你的bean可以注册多次,这时需要用的@Primary来确定你要哪个实现;一般来说,对于自定义的配置类,我们应该加上@ConditionalOnMissingBean注解,以避免多个配置同时注入的风险。
@ConditionalOnMissingBean(value = AmazonS3.class)
@ConditionalOnProperty(name = {"amazonaws.s3.endpoint", "amazonaws.s3.access-key", "amazonaws.s3.secret-key"})
public class AmazonS3AutoConfiguration {

    @Autowired
    private AmazonS3Properties amazonS3Properties;

    @Bean
    public AmazonS3 amazonS3() {
        ClientConfiguration clientConfig = new ClientConfiguration();
        clientConfig.setProtocol("https".equalsIgnoreCase(amazonS3Properties.getProtocol()) ? Protocol.HTTPS : Protocol.HTTP);
        clientConfig.setMaxConnections(amazonS3Properties.getMaxConnections());
        clientConfig.setConnectionTimeout(amazonS3Properties.getConnectionTimeout());
        clientConfig.setSocketTimeout(amazonS3Properties.getSocketTimeout());
        clientConfig.setUseGzip(amazonS3Properties.isUseGzip());
        BasicAWSCredentials basicAWSCredentials = new BasicAWSCredentials(amazonS3Properties.getAccessKey(), amazonS3Properties.getSecretKey());
        AmazonS3 amazonS3 = new AmazonS3Client(basicAWSCredentials, clientConfig);
        amazonS3.setEndpoint(amazonS3Properties.getEndpoint());
        return amazonS3;
    }
}

4、指定AmazonS3FactoryBean获取bean对象


import com.amazonaws.ClientConfiguration;
import com.amazonaws.Protocol;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import lombok.Data;
import org.springframework.beans.factory.FactoryBean;

/**
 * @author chenxiaolei
 */
@Data
public class AmazonS3FactoryBean implements FactoryBean<AmazonS3> {

    private String endpoint;

    private String accessKey;

    private String secretKey;

    private String protocol = "http";

    private int maxConnections = ClientConfiguration.DEFAULT_MAX_CONNECTIONS;

    private int connectionTimeout = ClientConfiguration.DEFAULT_CONNECTION_TIMEOUT;

    private int socketTimeout = ClientConfiguration.DEFAULT_SOCKET_TIMEOUT;

    private boolean useGzip = ClientConfiguration.DEFAULT_USE_GZIP;


    @Override
    public AmazonS3 getObject() {
        ClientConfiguration clientConfig = new ClientConfiguration();
        clientConfig.setProtocol("https".equalsIgnoreCase(getProtocol()) ? Protocol.HTTPS : Protocol.HTTP);
        clientConfig.setMaxConnections(getMaxConnections());
        clientConfig.setConnectionTimeout(getConnectionTimeout());
        clientConfig.setSocketTimeout(getSocketTimeout());
        clientConfig.setUseGzip(isUseGzip());
        BasicAWSCredentials basicAWSCredentials = new BasicAWSCredentials(getAccessKey(), getSecretKey());
        AmazonS3 amazonS3 = new AmazonS3Client(basicAWSCredentials, clientConfig);
        amazonS3.setEndpoint(getEndpoint());
        return amazonS3;
    }

    @Override
    public Class<?> getObjectType() {
        return AmazonS3.class;
    }
}

5、controller或者service添加配置 

@Value("${amazonaws.s3.bucketName}")
private String bucketName;
@Value("${amazonaws.s3.templateKey}")
private String templateKey;
@Value("${amazonaws.s3.templatePath}")
private String templatePath;
@Autowired
private AmazonS3 s3Client;

6、编写AmazonS3Util

package com.chuangzhen.dayu.common.util;

import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.model.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;


/**
 * @Description AmazonS3存储工具类
 * @Author pizilei
 * @Date 2021/10/29 13:36
 **/
@Slf4j
public class AmazonS3Util {

    /**
     * 获取Bucket
     *
     * @param s3Client   S3客户端
     * @param bucketName 桶名
     * @return
     */
    public static Bucket getBucket(AmazonS3 s3Client, String bucketName) {
        Bucket bucket = null;
        List<Bucket> buckets = s3Client.listBuckets();
        for (Bucket b : buckets) {
            if (b.getName().equals(bucketName)) {
                bucket = b;
            }
        }
        return bucket;

    }

    /**
     * 创建Bucket
     *
     * @param s3Client   S3客户端
     * @param bucketName 桶名
     * @return
     */
    public static Bucket createBucket(AmazonS3 s3Client, String bucketName) {
        Bucket bucket = null;
        if (s3Client.doesBucketExistV2(bucketName)) {
            log.info("Bucket:{} already exists.", bucketName);
            bucket = getBucket(s3Client, bucketName);
        } else {
            try {
                bucket = s3Client.createBucket(bucketName);
            } catch (Exception e) {
                log.error("createBucket", e.getMessage());
            }
        }
        return bucket;

    }

    /**
     * 文件上传
     *
     * @param s3Client   S3客户端
     * @param bucketName 桶名
     * @param filePath   /data
     * @param fileName   文件名
     * @throws Exception
     */
    public static void upload(AmazonS3 s3Client, String bucketName, String filePath, String fileName) throws Exception {
        Bucket bucket = createBucket(s3Client, bucketName);
        if (bucket == null) {
            log.warn("存储桶{}创建失败", bucketName);
            return;
        }
        File file = new File(filePath);
        log.info("AmazonS3 file:{},key:{}", file, fileName);
        s3Client.putObject(bucketName, fileName, file);
    }


    /**
     * 上传
     *
     * @param s3Client      S3客户端
     * @param bucketName    桶名
     * @param fileName      文件名
     * @param multipartFile MultipartFile
     * @return
     * @throws Exception
     */
    public static PutObjectResult upload(AmazonS3 s3Client, String bucketName, String fileName, MultipartFile multipartFile) throws Exception {
        Bucket bucket = createBucket(s3Client, bucketName);
        if (bucket == null) {
            log.warn("存储桶{}创建失败", bucketName);
            return null;
        }
        ObjectMetadata metadata = new ObjectMetadata();
        metadata.setContentType(multipartFile.getContentType());
        metadata.setContentLength(multipartFile.getSize());
        PutObjectResult putResult = s3Client.putObject(bucketName, fileName, multipartFile.getInputStream(), metadata);
        log.info("【流方式】上传MultipartFile完成,md5:{},S3文件:{}", putResult.getETag(), fileName);
        return putResult;
    }


    /**
     * 文件下载
     *
     * @param s3Client       S3客户端
     * @param bucketName     桶名
     * @param fileName       文件名
     * @param targetFilePath 目标路径 例如:"E:\\data\\血缘解析Excel模板1个上中下游_466.xlsx"
     */
    public static void downloadFile(AmazonS3 s3Client, String bucketName, String fileName, String targetFilePath) {
        S3Object object = s3Client.getObject(new GetObjectRequest(bucketName, fileName));
        if (object != null) {
            byte[] data = null;
            try (InputStream input = object.getObjectContent(); FileOutputStream fileOutputStream = new FileOutputStream(targetFilePath)) {
                data = new byte[input.available()];
                int len = 0;
                while ((len = input.read(data)) != -1) {
                    fileOutputStream.write(data, 0, len);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    /**
     * 文件读取流
     *
     * @param s3Client   S3客户端
     * @param bucketName 桶名
     * @param fileName   文件名
     * @return
     * @throws IOException
     */
    public static InputStream readContent(AmazonS3 s3Client, String bucketName, String fileName) throws IOException {
        S3Object object = s3Client.getObject(new GetObjectRequest(bucketName, fileName));
        return object.getObjectContent();
    }

}

;