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

}

  • 3
    点赞
  • 34
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
Spring Boot可以使用Amazon S3进行对象存储。要在Spring Boot中使用Amazon S3,你需要进行以下步骤: 1. 添加Amazon S3的依赖:在你的项目的pom.xml文件中添加Amazon S3的依赖项。你可以在Maven仓库中找到相应的依赖项,然后将其添加到你的pom.xml文件中。 2. 定义一个抽象类:创建一个抽象类来定义与Amazon S3交互的方法。该抽象类可以包含上传文件、授权、删除文件等方法。 3. 实现具体的类:继承抽象类并实现其中的方法。在实现类中,你可以使用AWS SDK for Java来与Amazon S3进行交互。可以使用SDK提供的方法来上传文件、获取授权、删除文件等操作。 4. 在Spring Boot中使用Amazon S3:将实现类引入到你的Spring Boot应用程序中,并使用注解或配置来将其配置为Amazon S3的客户端。你可以在需要使用Amazon S3的地方使用该客户端来执行相应的操作。 总结起来,使用Spring Boot和Amazon S3进行对象存储的步骤是: 1. 添加Amazon S3的依赖。 2. 定义抽象类来定义与Amazon S3交互的方法。 3. 实现具体的类并使用AWS SDK for Java来与Amazon S3进行交互。 4. 在Spring Boot中使用该实现类来执行相应的操作。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [SpringBoot整合亚马逊S3](https://blog.csdn.net/u010953816/article/details/123354144)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

莱恩大数据

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值