Initial commit
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>oneone-common</artifactId>
|
||||
<groupId>com.oneone.cloud</groupId>
|
||||
<version>${revision}</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>common-oss</artifactId>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>8</maven.compiler.source>
|
||||
<maven.compiler.target>8</maven.compiler.target>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.oneone.cloud</groupId>
|
||||
<artifactId>common-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.aliyun.oss</groupId>
|
||||
<artifactId>aliyun-sdk-oss</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
package com.oneone.common.oss;
|
||||
|
||||
import com.oneone.common.oss.ali.AliClient;
|
||||
import com.oneone.common.oss.ali.OSSProperties;
|
||||
import com.oneone.common.oss.ali.OSSUtil;
|
||||
import feign.Client;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.cloud.client.loadbalancer.LoadBalancedRetryFactory;
|
||||
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* @author mice
|
||||
* @version 1.0
|
||||
* @date 2022-03-22 17:58
|
||||
*/
|
||||
@Configuration
|
||||
@RequiredArgsConstructor
|
||||
@Slf4j
|
||||
public class FileServerAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
@ConditionalOnProperty(value = "file-server-client.type", havingValue = "ali")
|
||||
public FileServerClient aliOssClient(OSSProperties ossProperties) {
|
||||
return new AliClient(new OSSUtil(ossProperties));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.oneone.common.oss;
|
||||
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.InputStream;
|
||||
|
||||
/**
|
||||
* @author mice
|
||||
* @version 1.0
|
||||
* @date 2023-02-07 19:45
|
||||
*/
|
||||
public interface FileServerClient {
|
||||
|
||||
String uploadFile(InputStream inputStream, String path, String fileName);
|
||||
|
||||
String uploadFile(InputStream inputStream, String path, String fileName, Long length);
|
||||
|
||||
|
||||
String uploadFile(MultipartFile file, String path, String fileName);
|
||||
|
||||
String uploadString(String txt, String path, String fileName);
|
||||
|
||||
|
||||
void deleteFile(String url);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package com.oneone.common.oss.ali;
|
||||
|
||||
import com.oneone.common.oss.FileServerClient;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.InputStream;
|
||||
|
||||
/**
|
||||
* @author mice
|
||||
* @version 1.0
|
||||
* @date 2023-02-07 19:51
|
||||
*/
|
||||
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class AliClient implements FileServerClient {
|
||||
|
||||
private OSSUtil ossUtil;
|
||||
|
||||
@Override
|
||||
public String uploadFile(InputStream inputStream, String path, String fileName) {
|
||||
return ossUtil.uploadFile2OSS(inputStream, path, fileName,null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String uploadFile(InputStream inputStream, String path, String fileName,Long length) {
|
||||
return ossUtil.uploadFile2OSS(inputStream, path, fileName,length);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String uploadFile(MultipartFile file, String path, String fileName) {
|
||||
return ossUtil.uploadImg2Oss(file, path, fileName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String uploadString(String txt, String path, String fileName) {
|
||||
return ossUtil.uploadString(txt, path, fileName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteFile(String url) {
|
||||
ossUtil.deleteFile(url);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.oneone.common.oss.ali;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@ConfigurationProperties(prefix = "file-server-client.ali")
|
||||
@ConditionalOnProperty(value = "file-server-client.type", havingValue = "ali")
|
||||
@Configuration
|
||||
@Data
|
||||
public class OSSProperties {
|
||||
|
||||
private String endpoint;
|
||||
|
||||
private String accessId;
|
||||
|
||||
private String accessKey;
|
||||
|
||||
private String bucket;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,205 @@
|
||||
package com.oneone.common.oss.ali;
|
||||
|
||||
import com.aliyun.oss.OSS;
|
||||
import com.aliyun.oss.OSSClientBuilder;
|
||||
import com.aliyun.oss.model.ObjectMetadata;
|
||||
import com.aliyun.oss.model.PutObjectResult;
|
||||
import com.oneone.common.exception.BusinessException;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.URL;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Date;
|
||||
|
||||
@Slf4j
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class OSSUtil {
|
||||
|
||||
private OSSProperties ossProperties;
|
||||
|
||||
/**
|
||||
* 删除单个图片
|
||||
*
|
||||
* @param url
|
||||
*/
|
||||
public void deleteFile(String url) {
|
||||
if (StringUtils.isEmpty(url) || !url.contains(ossProperties.getEndpoint())) {
|
||||
return;
|
||||
}
|
||||
OSS ossClient = new OSSClientBuilder().build(ossProperties.getEndpoint(), ossProperties.getAccessId(), ossProperties.getAccessKey());
|
||||
url = StringUtils.substringAfterLast(url, ossProperties.getEndpoint() + "/");
|
||||
ossClient.deleteObject(ossProperties.getBucket(), url);
|
||||
log.warn("删除 " + url + " 成功");
|
||||
//ossClient.shutdown();
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传图片
|
||||
*
|
||||
* @param file
|
||||
* @param path
|
||||
* @param fileName
|
||||
* @return
|
||||
* @throws IOException
|
||||
*/
|
||||
public String uploadImg2Oss(MultipartFile file, String path, String fileName, Long length) {
|
||||
try {
|
||||
InputStream inputStream = file.getInputStream();
|
||||
this.uploadFile2OSS(inputStream, path, fileName, length);
|
||||
String url = "https://" + ossProperties.getBucket() + "." + ossProperties.getEndpoint() + "/" + path + fileName;
|
||||
return url;
|
||||
} catch (Exception e) {
|
||||
log.error("图片上传失败", e);
|
||||
throw new BusinessException("图片上传失败");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传图片
|
||||
*
|
||||
* @param file
|
||||
* @param path
|
||||
* @param fileName
|
||||
* @return
|
||||
* @throws IOException
|
||||
*/
|
||||
public String uploadImg2Oss(MultipartFile file, String path, String fileName) {
|
||||
try {
|
||||
InputStream inputStream = file.getInputStream();
|
||||
this.uploadFile2OSS(inputStream, path, fileName, null);
|
||||
String url = "https://" + ossProperties.getBucket() + "." + ossProperties.getEndpoint() + "/" + path + fileName;
|
||||
return url;
|
||||
} catch (Exception e) {
|
||||
log.error("图片上传失败", e);
|
||||
throw new BusinessException("图片上传失败");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传文本
|
||||
*
|
||||
* @param txt
|
||||
* @param path
|
||||
* @param fileName
|
||||
* @return
|
||||
* @throws IOException
|
||||
*/
|
||||
public String uploadString(String txt, String path, String fileName) {
|
||||
try {
|
||||
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(txt.getBytes(StandardCharsets.UTF_8));
|
||||
this.uploadFile2OSS(byteArrayInputStream, path, fileName, null);
|
||||
String url = "https://" + ossProperties.getBucket() + "." + ossProperties.getEndpoint() + "/" + path + fileName;
|
||||
return url;
|
||||
} catch (Exception e) {
|
||||
log.error("图片上传失败", e);
|
||||
throw new BusinessException("图片上传失败");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传到OSS服务器 如果同名文件会覆盖服务器上的
|
||||
*
|
||||
* @param instream 文件流
|
||||
* @param fileName 文件名称 包括后缀名
|
||||
* @return 出错返回"" ,唯一MD5数字签名
|
||||
*/
|
||||
public String uploadFile2OSS(InputStream instream, String path, String fileName, Long length) {
|
||||
|
||||
String ret = "";
|
||||
try {
|
||||
OSS ossClient = new OSSClientBuilder().build(ossProperties.getEndpoint(), ossProperties.getAccessId(), ossProperties.getAccessKey());
|
||||
// 创建上传Object的Metadata
|
||||
ObjectMetadata objectMetadata = new ObjectMetadata();
|
||||
objectMetadata.setContentLength(instream.available());
|
||||
objectMetadata.setCacheControl("no-cache");
|
||||
objectMetadata.setHeader("Pragma", "no-cache");
|
||||
objectMetadata.setContentType(getcontentType(fileName.substring(fileName.lastIndexOf(".") + 1)));
|
||||
objectMetadata.setContentDisposition("inline;filename=" + fileName);
|
||||
if (length != null && length > 0) {
|
||||
objectMetadata.setContentLength(length);
|
||||
}
|
||||
// 上传文件
|
||||
PutObjectResult putResult = ossClient.putObject(ossProperties.getBucket(), path + fileName, instream, objectMetadata);
|
||||
String url = "https://" + this.ossProperties.getBucket() + "." + this.ossProperties.getEndpoint() + "/" + path + fileName;
|
||||
return url;
|
||||
} catch (IOException e) {
|
||||
log.error(e.getMessage(), e);
|
||||
} finally {
|
||||
try {
|
||||
if (instream != null) {
|
||||
instream.close();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* Description: 判断OSS服务文件上传时文件的contentType
|
||||
*/
|
||||
public static String getcontentType(String filenameExtension) {
|
||||
if (filenameExtension.equalsIgnoreCase("bmp")) {
|
||||
return "image/bmp";
|
||||
}
|
||||
if (filenameExtension.equalsIgnoreCase("gif")) {
|
||||
return "image/gif";
|
||||
}
|
||||
if (filenameExtension.equalsIgnoreCase("json")) {
|
||||
return "application/json";
|
||||
}
|
||||
if (filenameExtension.equalsIgnoreCase("jpeg") || filenameExtension.equalsIgnoreCase("jpg")
|
||||
|| filenameExtension.equalsIgnoreCase("png")) {
|
||||
return "image/jpg";
|
||||
}
|
||||
if (filenameExtension.equalsIgnoreCase("html")) {
|
||||
return "text/html";
|
||||
}
|
||||
if (filenameExtension.equalsIgnoreCase("txt")) {
|
||||
return "text/plain";
|
||||
}
|
||||
if (filenameExtension.equalsIgnoreCase("vsd")) {
|
||||
return "application/vnd.visio";
|
||||
}
|
||||
if (filenameExtension.equalsIgnoreCase("pptx") || filenameExtension.equalsIgnoreCase("ppt")) {
|
||||
return "application/vnd.ms-powerpoint";
|
||||
}
|
||||
if (filenameExtension.equalsIgnoreCase("docx") || filenameExtension.equalsIgnoreCase("doc")) {
|
||||
return "application/msword";
|
||||
}
|
||||
if (filenameExtension.equalsIgnoreCase("xml")) {
|
||||
return "text/xml";
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得url链接
|
||||
*
|
||||
* @param key
|
||||
* @return
|
||||
*/
|
||||
public String getUrl(String key) {
|
||||
// 设置URL过期时间为10年 3600l* 1000*24*365*10
|
||||
OSS ossClient = new OSSClientBuilder().build(ossProperties.getEndpoint(), ossProperties.getAccessId(), ossProperties.getAccessKey());
|
||||
Date expiration = new Date(System.currentTimeMillis() + 3600L * 1000 * 24 * 365 * 10);
|
||||
// 生成URL
|
||||
URL url = ossClient.generatePresignedUrl(ossProperties.getBucket(), key, expiration);
|
||||
// url = "https://" + bucketName + ".oss-cn-beijing.aliyuncs.com/" + bucketName+"/"+ key;
|
||||
if (url != null) {
|
||||
String host = "https://" + url.getHost() + url.getPath();
|
||||
return host;
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||
com.oneone.common.oss.FileServerAutoConfiguration,\
|
||||
com.oneone.common.oss.ali.OSSProperties
|
||||
|
||||
|
||||
Reference in New Issue
Block a user