Skip to content

✨ 新增AwsS3 #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 38 additions & 3 deletions magic-starter-oss/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

## 简介

`magic-starter-oss` 主要是对一些常用的对象存储的封装,支持`阿里云OSS、腾讯云COS、七牛云存储、MinIO`。
`magic-starter-oss` 主要是对一些常用的对象存储的封装,支持`阿里云OSS、腾讯云COS、七牛云存储、MinIO`、`AwsS3`

## 使用

Expand All @@ -25,12 +25,13 @@

## 快速上手

> magic-starter-oss 提供了 4 种常见的对象云存储的支持。
> magic-starter-oss 提供了 5 种常见的对象云存储的支持。
>
> 1. 阿里云 OSS
> 2. 腾讯云 COS
> 3. 七牛云存储
> 4. MinIO 自建
> 5. AwsS3 服务
>
> 懒人请直接看demo:https://github.com/xkcoding/magic-starter-oss-demo

Expand Down Expand Up @@ -140,6 +141,33 @@ magic:
endpoint: http://192.168.31.8:9000
```

#### AwsS3

##### 引入依赖

```xml
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-s3</artifactId>
<version>${awss3.oss.version}</version>
</dependency>
```

##### 配置文件

```yaml
magic:
oss:
aws-s3:
enabled: true
access-key: accesskey
secret-key: secretkey
bucket-name: tmp
endpoint: https://***.amazonaws.com
https: true
region: xxx-west-1 # 自建服务,可不填
```

### 使用

#### 阿里云 OSS
Expand Down Expand Up @@ -170,8 +198,15 @@ private QiNiuCloudTemplate qiNiuCloudTemplate;
private MinIoTemplate minIoTemplate;
```

#### AwsS3

```java
@Autowired
private AwsS3Template awsS3Template;
```

## 特点

- 提供了存储桶生成策略、及文件名生成策略,用户自定义只需要自定义 `com.xkcoding.magic.oss.support.rule.OssRule`,同时加入到 Spring 容器中即可
- 提供了统一的操作接口,`com.xkcoding.magic.oss.OssTemplate` 后续集成其他对象存储,只需要实现该接口即可
- 虽然有 4 种实现,但是对外暴露的 API 一致,减去记忆的烦恼
- 虽然有 5 种实现,但是对外暴露的 API 一致,减去记忆的烦恼
9 changes: 8 additions & 1 deletion magic-starter-oss/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,15 @@
<packaging>jar</packaging>

<description>
Magic Oss 集成多种对象存储,包括七牛云bucket、阿里云OSS、腾讯云COS、minio
Magic Oss 集成多种对象存储,包括七牛云bucket、阿里云OSS、腾讯云COS、minio、AwsS3
</description>

<properties>
<aliyun.oss.version>3.1.0</aliyun.oss.version>
<qcloud.oss.version>5.6.8</qcloud.oss.version>
<qiniu.oss.version>7.2.23</qiniu.oss.version>
<minio.oss.version>6.0.7</minio.oss.version>
<awss3.oss.version>1.11.782</awss3.oss.version>
</properties>

<dependencies>
Expand Down Expand Up @@ -80,6 +81,12 @@
<version>${minio.oss.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-s3</artifactId>
<version>${awss3.oss.version}</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>com.xkcoding.magic</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ protected String getBucketName() {
defaultBucketName = ossProperties.getMinIo()
.getBucketName();
break;
case AWSS3:
defaultBucketName = ossProperties.getAwsS3()
.getBucketName();
break;
default:
defaultBucketName = MagicConsts.MAGIC;
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ public class OssProperties {
* MinIO
*/
private MinIoProperties minIo = new MinIoProperties();
/**
* AwsS3
*/
private AwsS3Properties awsS3 = new AwsS3Properties();

/**
* 阿里云 OSS 配置
Expand Down Expand Up @@ -104,6 +108,24 @@ public static class QiNiuCloudProperties extends CommonProperties {
public static class MinIoProperties extends CommonProperties {
}

/**
* AwsS3 配置
*/
@Data
@EqualsAndHashCode(callSuper = true)
public static class AwsS3Properties extends CommonProperties {
/**
* 是否使用 https
*/
private Boolean https = false;

/**
* 设置区,自建服务,可不填
* the region to use for SigV4 signing of requests (e.g. us-west-1)
*/
private String region;
}

/**
* 通用配置
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* Copyright (c) 2019-2029, xkcoding & Yangkai.Shen & 沈扬凯 (237497819@qq.com & xkcoding.com).
* <p>
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.gnu.org/licenses/lgpl.html
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.xkcoding.magic.oss.autoconfigure.awss3;

import com.amazonaws.ClientConfiguration;
import com.amazonaws.Protocol;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.client.builder.AwsClientBuilder;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.xkcoding.magic.oss.autoconfigure.OssAutoConfiguration;
import com.xkcoding.magic.oss.autoconfigure.OssProperties;
import com.xkcoding.magic.oss.support.awss3.AwsS3Template;
import com.xkcoding.magic.oss.support.rule.OssRule;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
* <p>
* 亚马逊 S3 存储自动装配类
* </p>
*
* @author harrylee
* @date Created in 2020/5/15 15:25
*/
@Configuration
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@AutoConfigureAfter(OssAutoConfiguration.class)
@ConditionalOnProperty(value = "magic.oss.aws-s3.enabled", havingValue = "true")
public class AwsS3AutoConfiguration {
private final OssProperties ossProperties;

@Bean
@SneakyThrows
@ConditionalOnMissingBean
public AmazonS3 awsS3Client() {
// 设置 AccessKey SecretKey
AWSStaticCredentialsProvider awsStaticCredentialsProvider = new AWSStaticCredentialsProvider(
new BasicAWSCredentials(ossProperties.getAwsS3()
.getAccessKey(), ossProperties.getAwsS3()
.getSecretKey()));

// 设置 Client
ClientConfiguration clientConfiguration = new ClientConfiguration()
.withProtocol(ossProperties.getAwsS3()
.getHttps() ? Protocol.HTTPS : Protocol.HTTP)
.withRequestTimeout(2000);

// 设置 Endpoint
AwsClientBuilder.EndpointConfiguration endpointConfiguration = new AwsClientBuilder.EndpointConfiguration(
ossProperties.getAwsS3().getEndpoint(),
ossProperties.getAwsS3().getRegion());

return AmazonS3ClientBuilder.standard()
.withCredentials(awsStaticCredentialsProvider)
.withClientConfiguration(clientConfiguration)
.withPathStyleAccessEnabled(true)
.withEndpointConfiguration(endpointConfiguration)
.build();
}

@Bean
@ConditionalOnMissingBean
@ConditionalOnBean({AmazonS3.class, OssRule.class})
public AwsS3Template minIoTemplate(AmazonS3 amazonS3, OssRule ossRule) {
return new AwsS3Template(amazonS3, ossProperties, ossRule);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

/**
* <p>
* 七牛云存储自动装配类
* MinIo 存储自动装配类
* </p>
*
* @author yangkai.shen
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,9 @@ public enum OssType {
/**
* MinIO
*/
MINIO;
MINIO,
/**
* AwsS3
*/
AWSS3;
}
Loading