Commit c8b335dc by 宋祥

1.OSS对接

parent 7236844f
......@@ -161,9 +161,16 @@
<artifactId>jxl</artifactId>
<version>2.6.12</version>
</dependency>
<!-- S3 OSS -->
<dependency>
<groupId>com.baosight.iplat4j</groupId>
<artifactId>oss-plugin</artifactId>
<version>7.0.0</version>
</dependency>
</dependencies>
<build>
<finalName>hpjx</finalName>
<resources>
<resource>
<directory>src/main/java</directory>
......
package com.baosight.hpjx.core.oss;
import com.amazonaws.ClientConfiguration;
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.baosight.hpjx.util.StringUtils;
import com.baosight.iplat4j.core.ioc.spring.PlatApplicationContext;
import lombok.extern.slf4j.Slf4j;
/**
* OSS凭证
*
* @author:songx
* @date:2023/8/28,11:26
*/
@Slf4j
public class OssClientUtils {
private OssClientUtils() {
}
/**
* 初始化配置信息(纯粹就打印日志而已,因为会加载该类的全局变量)
*/
public static void init() {
AmazonS3 s3Client = Instance.s3Client;
log.info("初始化OSS客户端完成:isReady -> {}", s3Client != null);
}
/**
* 获取S3客户端
*
* @return
*/
public static AmazonS3 getS3Client() {
return Instance.s3Client;
}
/**
* 刷新客户端
*/
public static void refresh() {
Instance.refresh();
}
/**
* 初始化Instance
*
* @author:songx
* @date:2022/4/2,9:58
*/
private static class Instance {
/**
* 客户端实例
*/
private static AmazonS3 s3Client = buildAmazonS3();
/**
* 刷新S3客户端
*/
private static void refresh() {
s3Client = buildAmazonS3();
log.info("刷新OSS客户端完成:isReady->{}", s3Client != null);
}
/**
* 获取AmazonS3Client实例
*
* @return
*/
private static synchronized AmazonS3 buildAmazonS3() {
String accessKey = PlatApplicationContext.getProperty("iplat4j.admin.objectStorage.s3.accessKey");
String secretKey = PlatApplicationContext.getProperty("iplat4j.admin.objectStorage.s3.secretKey");
String endpoint = PlatApplicationContext.getProperty("iplat4j.admin.objectStorage.s3.endpoint");
if (StringUtils.isBlank(accessKey) || StringUtils.isBlank(secretKey) || StringUtils.isBlank(endpoint)) {
return null;
}
AmazonS3ClientBuilder client = AmazonS3ClientBuilder.standard();
ClientConfiguration config = new ClientConfiguration();
config.setProtocol(com.amazonaws.Protocol.HTTPS);
config.setConnectionTimeout(10001);
config.setSignerOverride("S3SignerType");
client.setClientConfiguration(config);
client.setCredentials(new AWSStaticCredentialsProvider(
new BasicAWSCredentials(accessKey, secretKey)));
String url = endpoint + ":" + 80;
client.setEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(endpoint, null));
return client.build();
}
}
}
package com.baosight.hpjx.core.oss;
import com.amazonaws.services.s3.AmazonS3;
import com.baosight.hpjx.util.AssertUtils;
import com.baosight.iplat4j.core.ioc.spring.PlatApplicationContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.PageContext;
import java.io.File;
/**
* OSS工具类
*
* @author:songx
* @date:2023/8/28,11:25
*/
public class OssUtils {
// 桶名称
private static final String bucketName = PlatApplicationContext.getProperty(
"iplat4j.admin.objectStorage.s3.bucket");
/**
* 前端上传文件
*
* @param pageContext
* @return
* @throws Exception
*/
public static String uploadFile(PageContext pageContext) throws Exception {
HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
return null;
}
/**
* 上传文件
*
* @param key 默认不指定key的情况下,以文件内容的hash值作为文件名
* @param file
*/
public static void uploadFile(String key, File file) {
AmazonS3 client = OssClientUtils.getS3Client();
AssertUtils.isNull(client, "上传失败,OSS客户端未初始化,请检查");
client.putObject(bucketName, key, file);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="platFileUploader" class="com.baosight.iplat4j.eu.dm.util.PlatFileUploader">
<property name="dao" ref="platDao"/>
</bean>
<bean id="fileUpLoadManager" class="com.baosight.iplat4j.eu.dm.PlatFileUploadManagerByAmazonS3">
<property name="fileUploader" ref="platFileUploader"/>
</bean>
</beans>
<%@ page contentType="text/html;charset=UTF-8" trimDirectiveWhitespaces="true" %>
<%@ page import="com.baosight.iplat4j.config.AdminFactoryConfig" %>
<%@ page import="com.baosight.iplat4j.eu.dm.util.IFileUploadManager" %>
<%@ page import="org.slf4j.LoggerFactory" %>
<%@ page import="org.slf4j.Logger" %>
<%
Logger log = LoggerFactory.getLogger(IFileUploadManager.class);
try {
String result = AdminFactoryConfig.getFileUpLoadInterface().uploadFile(pageContext);
log.info("uploadFile result:{}", result);
} catch (Exception e) {
log.error("附件上传时出现异常{}", e.getMessage(), e);
response.sendError(500, e.getMessage());
return;
}
%>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment