mirror of
https://gitee.com/hhyykk/ipms-sjy.git
synced 2025-02-15 01:54:58 +08:00
S3 minio sdk 替换为 aws sdk
This commit is contained in:
parent
d97ba8d4d8
commit
ad00fbc566
@ -116,8 +116,8 @@
|
|||||||
<artifactId>jsch</artifactId> <!-- 文件客户端:解决 sftp 连接 -->
|
<artifactId>jsch</artifactId> <!-- 文件客户端:解决 sftp 连接 -->
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>io.minio</groupId>
|
<groupId>com.amazonaws</groupId>
|
||||||
<artifactId>minio</artifactId> <!-- 文件客户端:解决阿里云、腾讯云、minio 等 S3 连接 -->
|
<artifactId>aws-java-sdk-s3</artifactId><!-- 文件客户端:解决阿里云、腾讯云、minio 等 S3 连接 -->
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
|
@ -4,10 +4,17 @@ import cn.hutool.core.io.IoUtil;
|
|||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
import cn.hutool.http.HttpUtil;
|
import cn.hutool.http.HttpUtil;
|
||||||
import cn.iocoder.yudao.module.infra.framework.file.core.client.AbstractFileClient;
|
import cn.iocoder.yudao.module.infra.framework.file.core.client.AbstractFileClient;
|
||||||
import io.minio.*;
|
import com.amazonaws.HttpMethod;
|
||||||
import io.minio.http.Method;
|
import com.amazonaws.auth.AWSStaticCredentialsProvider;
|
||||||
|
import com.amazonaws.auth.BasicAWSCredentials;
|
||||||
|
import com.amazonaws.client.builder.AwsClientBuilder;
|
||||||
|
import com.amazonaws.services.s3.*;
|
||||||
|
import com.amazonaws.services.s3.model.ObjectMetadata;
|
||||||
|
import com.amazonaws.services.s3.model.S3Object;
|
||||||
|
|
||||||
|
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
|
import java.util.Date;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -19,7 +26,7 @@ import java.util.concurrent.TimeUnit;
|
|||||||
*/
|
*/
|
||||||
public class S3FileClient extends AbstractFileClient<S3FileClientConfig> {
|
public class S3FileClient extends AbstractFileClient<S3FileClientConfig> {
|
||||||
|
|
||||||
private MinioClient client;
|
private AmazonS3Client client;
|
||||||
|
|
||||||
public S3FileClient(Long id, S3FileClientConfig config) {
|
public S3FileClient(Long id, S3FileClientConfig config) {
|
||||||
super(id, config);
|
super(id, config);
|
||||||
@ -32,26 +39,57 @@ public class S3FileClient extends AbstractFileClient<S3FileClientConfig> {
|
|||||||
config.setDomain(buildDomain());
|
config.setDomain(buildDomain());
|
||||||
}
|
}
|
||||||
// 初始化客户端
|
// 初始化客户端
|
||||||
client = MinioClient.builder()
|
|
||||||
.endpoint(buildEndpointURL()) // Endpoint URL
|
client = (AmazonS3Client)AmazonS3ClientBuilder.standard()
|
||||||
.region(buildRegion()) // Region
|
.withCredentials(buildCredentials())
|
||||||
.credentials(config.getAccessKey(), config.getAccessSecret()) // 认证密钥
|
.withEndpointConfiguration(buildEndpointConfiguration())
|
||||||
.build();
|
.build();
|
||||||
enableVirtualStyleEndpoint();
|
|
||||||
|
// enableVirtualStyleEndpoint();
|
||||||
|
|
||||||
|
// client = AmazonS3ClientBuilder.builder()
|
||||||
|
// .endpoint(buildEndpointURL()) // Endpoint URL
|
||||||
|
// .region(buildRegion()) // Region
|
||||||
|
// .credentials(config.getAccessKey(), config.getAccessSecret()) // 认证密钥
|
||||||
|
// .build();
|
||||||
|
// enableVirtualStyleEndpoint();
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* 基于config秘钥 构建 S3 客户端的认证信息
|
||||||
|
*
|
||||||
|
* @return S3 客户端的认证信息
|
||||||
|
*/
|
||||||
|
private AWSStaticCredentialsProvider buildCredentials() {
|
||||||
|
AWSStaticCredentialsProvider awsStaticCredentialsProvider = new AWSStaticCredentialsProvider(
|
||||||
|
new BasicAWSCredentials(config.getAccessKey(), config.getAccessSecret()));
|
||||||
|
return awsStaticCredentialsProvider;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* 构建 S3 客户端的 Endpoint 配置包括 region、endpoint
|
||||||
|
*
|
||||||
|
* @return S3 客户端的 EndpointConfiguration 配置
|
||||||
|
*/
|
||||||
|
private AwsClientBuilder.EndpointConfiguration buildEndpointConfiguration() {
|
||||||
|
AwsClientBuilder.EndpointConfiguration endpointConfiguration = new AwsClientBuilder.EndpointConfiguration(
|
||||||
|
config.getEndpoint(), buildRegion());
|
||||||
|
return endpointConfiguration;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 基于 endpoint 构建调用云服务的 URL 地址
|
|
||||||
*
|
|
||||||
* @return URI 地址
|
// /**
|
||||||
*/
|
// * 基于 endpoint 构建调用云服务的 URL 地址
|
||||||
private String buildEndpointURL() {
|
// *
|
||||||
// 如果已经是 http 或者 https,则不进行拼接.主要适配 MinIO
|
// * @return URI 地址
|
||||||
if (HttpUtil.isHttp(config.getEndpoint()) || HttpUtil.isHttps(config.getEndpoint())) {
|
// */
|
||||||
return config.getEndpoint();
|
// private String buildEndpointURL() {
|
||||||
}
|
// // 如果已经是 http 或者 https,则不进行拼接.主要适配 MinIO
|
||||||
return StrUtil.format("https://{}", config.getEndpoint());
|
// if (HttpUtil.isHttp(config.getEndpoint()) || HttpUtil.isHttps(config.getEndpoint())) {
|
||||||
}
|
// return config.getEndpoint();
|
||||||
|
// }
|
||||||
|
// return StrUtil.format("https://{}", config.getEndpoint());
|
||||||
|
// }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 基于 bucket + endpoint 构建访问的 Domain 地址
|
* 基于 bucket + endpoint 构建访问的 Domain 地址
|
||||||
@ -88,55 +126,69 @@ public class S3FileClient extends AbstractFileClient<S3FileClientConfig> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 开启 VirtualStyle 模式
|
* 开启 PathStyle模式
|
||||||
*/
|
*/
|
||||||
private void enableVirtualStyleEndpoint() {
|
private void enableVirtualStyleEndpoint() {
|
||||||
if (StrUtil.containsAny(config.getEndpoint(),
|
// if (StrUtil.containsAny(config.getEndpoint(),
|
||||||
S3FileClientConfig.ENDPOINT_TENCENT, // 腾讯云 https://cloud.tencent.com/document/product/436/41284
|
// S3FileClientConfig.ENDPOINT_TENCENT, // 腾讯云 https://cloud.tencent.com/document/product/436/41284
|
||||||
S3FileClientConfig.ENDPOINT_VOLCES)) { // 火山云 https://www.volcengine.com/docs/6349/1288493
|
// S3FileClientConfig.ENDPOINT_VOLCES)) { // 火山云 https://www.volcengine.com/docs/6349/1288493
|
||||||
client.enableVirtualStyleEndpoint();
|
//
|
||||||
}
|
// }
|
||||||
|
S3ClientOptions clientOptions = S3ClientOptions.builder()
|
||||||
|
.setPathStyleAccess(true)
|
||||||
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String upload(byte[] content, String path, String type) throws Exception {
|
public String upload(byte[] content, String path, String type) throws Exception {
|
||||||
// 执行上传
|
// 执行上传
|
||||||
client.putObject(PutObjectArgs.builder()
|
ObjectMetadata objectMetadata = new ObjectMetadata();
|
||||||
.bucket(config.getBucket()) // bucket 必须传递
|
objectMetadata.setContentType(type);
|
||||||
.contentType(type)
|
|
||||||
.object(path) // 相对路径作为 key
|
client.putObject(config.getBucket(), path, new ByteArrayInputStream(content), objectMetadata);
|
||||||
.stream(new ByteArrayInputStream(content), content.length, -1) // 文件内容
|
// client.putObject(PutObjectArgs.builder()
|
||||||
.build());
|
// .bucket(config.getBucket()) // bucket 必须传递
|
||||||
|
// .contentType(type)
|
||||||
|
// .object(path) // 相对路径作为 key
|
||||||
|
// .stream(new ByteArrayInputStream(content), content.length, -1) // 文件内容
|
||||||
|
// .build());
|
||||||
// 拼接返回路径
|
// 拼接返回路径
|
||||||
return config.getDomain() + "/" + path;
|
return config.getDomain() + "/" + path;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void delete(String path) throws Exception {
|
public void delete(String path) throws Exception {
|
||||||
client.removeObject(RemoveObjectArgs.builder()
|
|
||||||
.bucket(config.getBucket()) // bucket 必须传递
|
client.deleteObject(config.getBucket(), path);
|
||||||
.object(path) // 相对路径作为 key
|
|
||||||
.build());
|
// client.removeObject(RemoveObjectArgs.builder()
|
||||||
|
// .bucket(config.getBucket()) // bucket 必须传递
|
||||||
|
// .object(path) // 相对路径作为 key
|
||||||
|
// .build());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public byte[] getContent(String path) throws Exception {
|
public byte[] getContent(String path) throws Exception {
|
||||||
GetObjectResponse response = client.getObject(GetObjectArgs.builder()
|
S3Object tempS3Object = client.getObject(config.getBucket(), path);
|
||||||
.bucket(config.getBucket()) // bucket 必须传递
|
// GetObjectResponse response = client.getObject(GetObjectArgs.builder()
|
||||||
.object(path) // 相对路径作为 key
|
// .bucket(config.getBucket()) // bucket 必须传递
|
||||||
.build());
|
// .object(path) // 相对路径作为 key
|
||||||
return IoUtil.readBytes(response);
|
// .build());
|
||||||
|
return IoUtil.readBytes(tempS3Object.getObjectContent());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public FilePresignedUrlRespDTO getPresignedObjectUrl(String path) throws Exception {
|
public FilePresignedUrlRespDTO getPresignedObjectUrl(String path) throws Exception {
|
||||||
String uploadUrl = client.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder()
|
//设定过期时间为24小时
|
||||||
.method(Method.PUT)
|
Date expiration = new Date(System.currentTimeMillis() + TimeUnit.HOURS.toMillis(24));
|
||||||
.bucket(config.getBucket())
|
String uploadUrl = String.valueOf(client.generatePresignedUrl(config.getBucket(), path,expiration , HttpMethod.PUT));
|
||||||
.object(path)
|
// String uploadUrl = client.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder()
|
||||||
.expiry(10, TimeUnit.MINUTES) // 过期时间(秒数)取值范围:1 秒 ~ 7 天
|
// .method(Method.PUT)
|
||||||
.build()
|
// .bucket(config.getBucket())
|
||||||
);
|
// .object(path)
|
||||||
|
// .expiry(10, TimeUnit.MINUTES) // 过期时间(秒数)取值范围:1 秒 ~ 7 天
|
||||||
|
// .build()
|
||||||
|
// );
|
||||||
return new FilePresignedUrlRespDTO(uploadUrl, config.getDomain() + "/" + path);
|
return new FilePresignedUrlRespDTO(uploadUrl, config.getDomain() + "/" + path);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user