Spring Boot 整合 AWS S3协议 OSS功能 支持 七牛、阿里、Minio等一切支持S3协议的云厂商
参考项目: RuoYi-Vue-Plus
依赖引入
<dependency><groupId>com.amazonaws</groupId><artifactId>aws-java-sdk-s3</artifactId><version>1.12.215</version></dependency>
配置OSS客户端
配置文件
oss:# 对应云厂商的站点域名endpoint: XXX.XXX.XXX# 对应云厂商桶的绑定域名domain: XXX.XXX.XXXaccessKey: XXXXXXXsecretKey: XXXXXXX# 存储空间名/桶名bucketName: LionLi# 存储区域region: XXX# 是否https(Y=是,N=否)isHttps: N
配置文件映射类 OssProperties
/*** OSS对象存储 配置属性** @author Lion Li*/
@Data
@Component
@ConfigurationProperties(prefix = "oss")
public class OssProperties {/*** 访问站点*/private String endpoint;/*** 自定义域名*/private String domain;/*** ACCESS_KEY*/private String accessKey;/*** SECRET_KEY*/private String secretKey;/*** 存储空间名*/private String bucketName;/*** 存储区域*/private String region;/*** 是否https(Y=是,N=否)*/private String isHttps;}
客户端配置类 OssConfig
/*** S3 存储协议 所有兼容S3协议的云厂商均支持* 阿里云 腾讯云 七牛云 minio** @author Lion Li*/
@Configuration
public class OssConfig {@Beanpublic AmazonS3 createOssClient(OssProperties properties) {AwsClientBuilder.EndpointConfiguration endpointConfig =new AwsClientBuilder.EndpointConfiguration(properties.getEndpoint(), properties.getRegion());AWSCredentials credentials = new BasicAWSCredentials(properties.getAccessKey(), properties.getSecretKey());AWSCredentialsProvider credentialsProvider = new AWSStaticCredentialsProvider(credentials);ClientConfiguration clientConfig = new ClientConfiguration();if (OssConstant.IS_HTTPS.equals(properties.getIsHttps())) {clientConfig.setProtocol(Protocol.HTTPS);} else {clientConfig.setProtocol(Protocol.HTTP);}return AmazonS3Client.builder().withEndpointConfiguration(endpointConfig).withClientConfiguration(clientConfig).withCredentials(credentialsProvider).disableChunkedEncoding().build();}}
以下方法通用代码
@Autowiredprivate OssProperties properties;@Autowiredprivate AmazonS3 client;
创建桶方法
String bucketName = properties.getBucketName();
if (client.doesBucketExistV2(bucketName)) {return;
}
CreateBucketRequest createBucketRequest = new CreateBucketRequest(bucketName);
createBucketRequest.setCannedAcl(CannedAccessControlList.PublicRead);
client.createBucket(createBucketRequest);
上传文件
try {ObjectMetadata metadata = new ObjectMetadata();metadata.setContentType(contentType);metadata.setContentLength(inputStream.available());client.putObject(new PutObjectRequest(properties.getBucketName(), "文件路径", inputStream, metadata));
} catch (Exception e) {e.printStackTrace();
}
删除文件
client.deleteObject(properties.getBucketName(), "文件路径");
更多使用越多协议文档
S3 协议文档
Spring Boot 整合 AWS S3协议 OSS功能 支持 七牛、阿里、Minio等一切支持S3协议的云厂商相关推荐
- spring boot 整合 阿里云oss上传
Spring Boot 整合 阿里云OSS上传 OSS申请和配置 1. 注册登录 2.开通以及配置 springboot整合使用 1. 进入我们springboot的项目中,导入oss相关依赖 2. ...
- 二、何为Spring Boot整合Spring Cloud?
题语:学习方法之多思考:正向.逆向.跳跃 作者:A哥(YourBatman) wx号:fsx641385712(备注"Java群"字样) 公众号:BAT的乌托邦(ID:BAT-ut ...
- 六、Spring Boot整合Shiro
六.Spring Boot整合Shiro 6.1.整合思路 6.2.创建spring boot项目 6.3.引入shiro依赖 6.4.配置shiro环境 创建配置类ShiroConfig 1.配置: ...
- Spring Boot: Spring Boot 整合 RabbitMQ
前言 RabbitMQ 是一个消息队列,说到消息队列,大家可能多多少少有听过,它主要的功能是用来实现应用服务的异步与解耦,同时也能起到削峰填谷.消息分发的作用. 消息队列在比较主要的一个作用是用来做应 ...
- VUE+Spring Boot整合MyBatis实现前后端分离项目壁纸网站
目录 前言 一.项目运行 二.环境需要 三.技术栈 四.项目说明 五.后端代码 前言 每次换桌面,壁纸总是不好找,搜索图片得不到好的索引与反馈,很难找到自己喜欢的壁纸,而壁纸网站可以免去我们去寻找壁纸 ...
- spring boot整合spring security笔记
最近自己做了一个小项目,正在进行springboot和spring Security的整合,有一丢丢的感悟,在这里分享一下: 首先,spring boot整合spring security最好是使用T ...
- RabbitMQ使用及与spring boot整合
1.MQ 消息队列(Message Queue,简称MQ)--应用程序和应用程序之间的通信方法 应用:不同进程Process/线程Thread之间通信 比较流行的中间件: ActiveMQ Rabbi ...
- 五、spring boot整合mybatis-plus
spring boot整合mybatis-plus 简介 mybatis 增强工具包,简化 CRUD 操作. 文档 http://mp.baomidou.com http://mybatis.plus ...
- spring boot 整合mybatis 无法输出sql的问题
使用spring boot整合mybatis,测试功能的时候,遇到到了sql问题,想要从日志上看哪里错了,但是怎么都无法输出执行的sql,我使用的是log4j2,百度了一下,很多博客都说,加上下面的日 ...
- springboot整合hibernate_峰哥说技术系列-17 .Spring Boot 整合 Spring Data JPA
今日份主题 Spring Boot 整合 Spring Data JPA JPA(Java Persistence API)是用于对象持久化的 API,是Java EE 5.0 平台标准的 ORM 规 ...
最新文章
- 初识mysql数据字段属性_MySQL数据库~~~~初识、基础数据类型
- 【计算理论】计算复杂性 ( 多项式等价 | P 类 | 丘奇-图灵论题延伸 )
- 阿里云centos 6.3 安装宝塔nginx面板无法远程mysql数据库和无法FTP连接问题解决方法
- 罗辑思维:情怀还是生意?
- VMware虚拟机网络模式详解 NAT模式
- Intellij IDEA集成JProfiler性能分析神器
- webpack的一些常用配置 (转)
- 您自己的MicroProfile Config来源
- javaScript学习笔记之运算符
- amazeui学习笔记--css(基本样式3)--文字排版Typography
- 最近重构公司消息服务的架构设计
- 如何在Linux开启HTTP服务,小技巧:如何快速开启一个静态 HTTP 服务?
- 有没有什么方法快速能找到导致软件崩溃的进程_手机软件闪退闪得怀疑人生?看我专治闪退二十年!...
- JSON与localStorage的爱恨情仇
- CDH页面中Oozie的调度告警邮箱设置
- 在Paint事件中绘制控件(边框)
- win7 professional 英文版 改 中文
- html5 调用摄像头 支持IE,ie调用不了摄像头 为什么电脑IE浏览器无法启动摄像头...
- 电动汽车充电站的最优选址matlab程序
- 《微观经济学》 第五章