initial clean deployment
This commit is contained in:
32
zzyl-oss/pom.xml
Normal file
32
zzyl-oss/pom.xml
Normal file
@@ -0,0 +1,32 @@
|
||||
<?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">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.zzyl</groupId>
|
||||
<artifactId>zzyl</artifactId>
|
||||
<version>3.8.8</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>zzyl-oss</artifactId>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>11</maven.compiler.source>
|
||||
<maven.compiler.target>11</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.aliyun.oss</groupId>
|
||||
<artifactId>aliyun-sdk-oss</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.zzyl</groupId>
|
||||
<artifactId>zzyl-common</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
69
zzyl-oss/src/main/java/com/zzyl/oss/AliyunOssProperties.java
Normal file
69
zzyl-oss/src/main/java/com/zzyl/oss/AliyunOssProperties.java
Normal file
@@ -0,0 +1,69 @@
|
||||
package com.zzyl.oss;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* @author ghy
|
||||
* @version V1.0
|
||||
* @date 2025-01-13 15:07
|
||||
*/
|
||||
@Component
|
||||
@ConfigurationProperties(prefix = "aliyun.oss")
|
||||
public class AliyunOssProperties {
|
||||
|
||||
private String domain;
|
||||
private String endpoint;
|
||||
private String bucketName;
|
||||
private String region;
|
||||
private String accessKeyId;
|
||||
private String secretAccessKey;
|
||||
|
||||
public String getDomain() {
|
||||
return domain;
|
||||
}
|
||||
|
||||
public void setDomain(String domain) {
|
||||
this.domain = domain;
|
||||
}
|
||||
|
||||
public String getEndpoint() {
|
||||
return endpoint;
|
||||
}
|
||||
|
||||
public void setEndpoint(String endpoint) {
|
||||
this.endpoint = endpoint;
|
||||
}
|
||||
|
||||
public String getBucketName() {
|
||||
return bucketName;
|
||||
}
|
||||
|
||||
public void setBucketName(String bucketName) {
|
||||
this.bucketName = bucketName;
|
||||
}
|
||||
|
||||
public String getRegion() {
|
||||
return region;
|
||||
}
|
||||
|
||||
public void setRegion(String region) {
|
||||
this.region = region;
|
||||
}
|
||||
|
||||
public String getAccessKeyId() {
|
||||
return accessKeyId;
|
||||
}
|
||||
|
||||
public void setAccessKeyId(String accessKeyId) {
|
||||
this.accessKeyId = accessKeyId;
|
||||
}
|
||||
|
||||
public String getSecretAccessKey() {
|
||||
return secretAccessKey;
|
||||
}
|
||||
|
||||
public void setSecretAccessKey(String secretAccessKey) {
|
||||
this.secretAccessKey = secretAccessKey;
|
||||
}
|
||||
}
|
||||
71
zzyl-oss/src/main/java/com/zzyl/oss/AliyunOssUtils.java
Normal file
71
zzyl-oss/src/main/java/com/zzyl/oss/AliyunOssUtils.java
Normal file
@@ -0,0 +1,71 @@
|
||||
package com.zzyl.oss;
|
||||
|
||||
import cn.hutool.core.lang.UUID;
|
||||
import cn.hutool.core.util.ObjUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.aliyun.oss.OSS;
|
||||
import com.aliyun.oss.OSSClientBuilder;
|
||||
import com.aliyun.oss.common.auth.CredentialsProvider;
|
||||
import com.aliyun.oss.common.auth.DefaultCredentialProvider;
|
||||
import com.zzyl.common.utils.DateUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
@Component
|
||||
public class AliyunOssUtils {
|
||||
|
||||
@Autowired
|
||||
private AliyunOssProperties aliyunOssProperties;
|
||||
|
||||
/**
|
||||
* 阿里云OSS 上传文件
|
||||
* @param file
|
||||
* @return 返回文件上传到服务器的路径
|
||||
*/
|
||||
public String uploadFile(MultipartFile file) {
|
||||
|
||||
String accessKeyId = aliyunOssProperties.getAccessKeyId();
|
||||
String secretAccessKey = aliyunOssProperties.getSecretAccessKey();
|
||||
String endpoint = aliyunOssProperties.getEndpoint();
|
||||
String region = aliyunOssProperties.getRegion();
|
||||
String bucketName = aliyunOssProperties.getBucketName();
|
||||
String domain = aliyunOssProperties.getDomain();
|
||||
|
||||
// 1.参数校验
|
||||
if(ObjUtil.isEmpty(file) || file.isEmpty()){
|
||||
throw new RuntimeException("上传文件不能为空");
|
||||
}
|
||||
String filename = file.getOriginalFilename();
|
||||
if(StrUtil.isEmpty(filename)){
|
||||
throw new RuntimeException("上传文件名错误!");
|
||||
}
|
||||
String path = null;
|
||||
OSS ossClient = null;
|
||||
try {
|
||||
CredentialsProvider credentialsProvider = new DefaultCredentialProvider(accessKeyId, secretAccessKey);
|
||||
ossClient = OSSClientBuilder.create()
|
||||
.endpoint(endpoint)
|
||||
.credentialsProvider(credentialsProvider)
|
||||
.credentialsProvider(credentialsProvider)
|
||||
.region(region)
|
||||
.build();
|
||||
//.jpg
|
||||
String suffix = filename.substring(filename.lastIndexOf("."));
|
||||
filename = DateUtils.datePath() + "/"
|
||||
+ UUID.randomUUID().toString().replaceAll("-", "")
|
||||
+ suffix;
|
||||
|
||||
ossClient.putObject(bucketName, filename, file.getInputStream());
|
||||
path = "https://" + bucketName + "." + domain + "/" + filename;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
if (ossClient != null) {
|
||||
ossClient.shutdown();
|
||||
}
|
||||
}
|
||||
return path;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user