mirror of
https://github.com/dromara/RuoYi-Vue-Plus.git
synced 2025-11-12 04:03:44 +08:00
update 编写 Minio 实现 调整OSS模块结构
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
package com.ruoyi.oss.service;
|
||||
|
||||
import com.ruoyi.oss.entity.UploadResult;
|
||||
|
||||
import java.io.InputStream;
|
||||
|
||||
/**
|
||||
@@ -30,7 +32,7 @@ public interface ICloudStorageService {
|
||||
* @param path 文件路径,包含文件名
|
||||
* @return 返回http地址
|
||||
*/
|
||||
String upload(byte[] data, String path);
|
||||
UploadResult upload(byte[] data, String path);
|
||||
|
||||
/**
|
||||
* 文件删除
|
||||
@@ -46,7 +48,7 @@ public interface ICloudStorageService {
|
||||
* @param suffix 后缀
|
||||
* @return 返回http地址
|
||||
*/
|
||||
String uploadSuffix(byte[] data, String suffix);
|
||||
UploadResult uploadSuffix(byte[] data, String suffix);
|
||||
|
||||
/**
|
||||
* 文件上传
|
||||
@@ -55,7 +57,7 @@ public interface ICloudStorageService {
|
||||
* @param path 文件路径,包含文件名
|
||||
* @return 返回http地址
|
||||
*/
|
||||
String upload(InputStream inputStream, String path);
|
||||
UploadResult upload(InputStream inputStream, String path);
|
||||
|
||||
/**
|
||||
* 文件上传
|
||||
@@ -64,5 +66,5 @@ public interface ICloudStorageService {
|
||||
* @param suffix 后缀
|
||||
* @return 返回http地址
|
||||
*/
|
||||
String uploadSuffix(InputStream inputStream, String suffix);
|
||||
UploadResult uploadSuffix(InputStream inputStream, String suffix);
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.core.io.IoUtil;
|
||||
import cn.hutool.core.util.IdUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.ruoyi.oss.entity.UploadResult;
|
||||
import com.ruoyi.oss.service.ICloudStorageService;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
|
||||
@@ -33,22 +34,22 @@ public abstract class AbstractCloudStorageService implements ICloudStorageServic
|
||||
}
|
||||
|
||||
@Override
|
||||
public abstract String upload(byte[] data, String path);
|
||||
public abstract UploadResult upload(byte[] data, String path);
|
||||
|
||||
@Override
|
||||
public abstract void delete(String path);
|
||||
|
||||
@Override
|
||||
public String upload(InputStream inputStream, String path) {
|
||||
public UploadResult upload(InputStream inputStream, String path) {
|
||||
byte[] data = IoUtil.readBytes(inputStream);
|
||||
return this.upload(data, path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public abstract String uploadSuffix(byte[] data, String suffix);
|
||||
public abstract UploadResult uploadSuffix(byte[] data, String suffix);
|
||||
|
||||
@Override
|
||||
public abstract String uploadSuffix(InputStream inputStream, String suffix);
|
||||
public abstract UploadResult uploadSuffix(InputStream inputStream, String suffix);
|
||||
|
||||
@Override
|
||||
public abstract void afterPropertiesSet() throws Exception;
|
||||
|
||||
@@ -3,6 +3,7 @@ package com.ruoyi.oss.service.impl;
|
||||
import com.aliyun.oss.ClientConfiguration;
|
||||
import com.aliyun.oss.OSSClient;
|
||||
import com.aliyun.oss.common.auth.DefaultCredentialProvider;
|
||||
import com.ruoyi.oss.entity.UploadResult;
|
||||
import com.ruoyi.oss.enumd.CloudServiceEnumd;
|
||||
import com.ruoyi.oss.exception.OssException;
|
||||
import com.ruoyi.oss.factory.OssFactory;
|
||||
@@ -49,18 +50,18 @@ public class AliyunCloudStorageServiceImpl extends AbstractCloudStorageService i
|
||||
}
|
||||
|
||||
@Override
|
||||
public String upload(byte[] data, String path) {
|
||||
public UploadResult upload(byte[] data, String path) {
|
||||
return upload(new ByteArrayInputStream(data), path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String upload(InputStream inputStream, String path) {
|
||||
public UploadResult upload(InputStream inputStream, String path) {
|
||||
try {
|
||||
client.putObject(this.properties.getBucketName(), path, inputStream);
|
||||
} catch (Exception e) {
|
||||
throw new OssException("上传文件失败,请检查配置信息");
|
||||
}
|
||||
return this.properties.getEndpoint() + "/" + path;
|
||||
return new UploadResult().setUrl(properties.getEndpoint() + "/" + path).setFilename(path);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -74,12 +75,12 @@ public class AliyunCloudStorageServiceImpl extends AbstractCloudStorageService i
|
||||
}
|
||||
|
||||
@Override
|
||||
public String uploadSuffix(byte[] data, String suffix) {
|
||||
public UploadResult uploadSuffix(byte[] data, String suffix) {
|
||||
return upload(data, getPath(this.properties.getPrefix(), suffix));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String uploadSuffix(InputStream inputStream, String suffix) {
|
||||
public UploadResult uploadSuffix(InputStream inputStream, String suffix) {
|
||||
return upload(inputStream, getPath(this.properties.getPrefix(), suffix));
|
||||
}
|
||||
|
||||
|
||||
@@ -1,17 +1,20 @@
|
||||
package com.ruoyi.oss.service.impl;
|
||||
|
||||
import com.ruoyi.oss.entity.UploadResult;
|
||||
import com.ruoyi.oss.enumd.CloudServiceEnumd;
|
||||
import com.ruoyi.oss.enumd.PolicyType;
|
||||
import com.ruoyi.oss.exception.OssException;
|
||||
import com.ruoyi.oss.factory.OssFactory;
|
||||
import com.ruoyi.oss.properties.CloudStorageProperties;
|
||||
import com.ruoyi.oss.properties.CloudStorageProperties.MinioProperties;
|
||||
import com.ruoyi.oss.service.abstractd.AbstractCloudStorageService;
|
||||
import io.minio.MinioClient;
|
||||
import io.minio.*;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.InputStream;
|
||||
|
||||
/**
|
||||
@@ -34,6 +37,16 @@ public class MinioCloudStorageServiceImpl extends AbstractCloudStorageService im
|
||||
.endpoint(this.properties.getEndpoint())
|
||||
.credentials(this.properties.getAccessKey(), this.properties.getSecretKey())
|
||||
.build();
|
||||
String bucketName = this.properties.getBucketName();
|
||||
boolean exists = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
|
||||
// 不存在就创建桶
|
||||
if (!exists) {
|
||||
minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
|
||||
minioClient.setBucketPolicy(SetBucketPolicyArgs.builder()
|
||||
.bucket(bucketName)
|
||||
.config(getPolicy(bucketName, PolicyType.READ))
|
||||
.build());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new IllegalArgumentException("Minio存储配置错误! 请检查系统配置!");
|
||||
}
|
||||
@@ -45,36 +58,125 @@ public class MinioCloudStorageServiceImpl extends AbstractCloudStorageService im
|
||||
}
|
||||
|
||||
@Override
|
||||
public String upload(byte[] data, String path) {
|
||||
try {
|
||||
public UploadResult upload(byte[] data, String path) {
|
||||
return upload(new ByteArrayInputStream(data), path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public UploadResult upload(InputStream inputStream, String path) {
|
||||
try {
|
||||
minioClient.putObject(PutObjectArgs.builder()
|
||||
.bucket(properties.getBucketName())
|
||||
.object(path)
|
||||
.contentType("application/octet-stream")
|
||||
.stream(inputStream, inputStream.available(), -1)
|
||||
.build());
|
||||
} catch (Exception e) {
|
||||
throw new OssException("上传文件失败,请核对Minio配置信息");
|
||||
}
|
||||
return this.properties.getEndpoint() + "/" + path;
|
||||
return new UploadResult().setUrl(getBaseUrl() + path).setFilename(path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(String path) {
|
||||
path = path.replace(getBaseUrl(), "");
|
||||
try {
|
||||
|
||||
minioClient.removeObject(RemoveObjectArgs.builder()
|
||||
.bucket(properties.getBucketName())
|
||||
.object(path)
|
||||
.build());
|
||||
} catch (Exception e) {
|
||||
throw new OssException(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String uploadSuffix(byte[] data, String suffix) {
|
||||
return upload(data, getPath(this.properties.getPrefix(), suffix));
|
||||
public UploadResult uploadSuffix(byte[] data, String suffix) {
|
||||
return upload(data, getPath("", suffix));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String uploadSuffix(InputStream inputStream, String suffix) {
|
||||
return upload(inputStream, getPath(this.properties.getPrefix(), suffix));
|
||||
public UploadResult uploadSuffix(InputStream inputStream, String suffix) {
|
||||
return upload(inputStream, getPath("", suffix));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
OssFactory.register(getServiceType(),this);
|
||||
OssFactory.register(getServiceType(), this);
|
||||
}
|
||||
|
||||
private String getBaseUrl() {
|
||||
return properties.getEndpoint() + "/" + properties.getBucketName() + "/";
|
||||
}
|
||||
|
||||
private String getPolicy(String bucketName, PolicyType policyType) {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
builder.append("{\n");
|
||||
builder.append(" \"Statement\": [\n");
|
||||
builder.append(" {\n");
|
||||
builder.append(" \"Action\": [\n");
|
||||
if (policyType == PolicyType.WRITE) {
|
||||
builder.append(" \"s3:GetBucketLocation\",\n");
|
||||
builder.append(" \"s3:ListBucketMultipartUploads\"\n");
|
||||
} else if (policyType == PolicyType.READ_WRITE) {
|
||||
builder.append(" \"s3:GetBucketLocation\",\n");
|
||||
builder.append(" \"s3:ListBucket\",\n");
|
||||
builder.append(" \"s3:ListBucketMultipartUploads\"\n");
|
||||
} else {
|
||||
builder.append(" \"s3:GetBucketLocation\"\n");
|
||||
}
|
||||
builder.append(" ],\n");
|
||||
builder.append(" \"Effect\": \"Allow\",\n");
|
||||
builder.append(" \"Principal\": \"*\",\n");
|
||||
builder.append(" \"Resource\": \"arn:aws:s3:::");
|
||||
builder.append(bucketName);
|
||||
builder.append("\"\n");
|
||||
builder.append(" },\n");
|
||||
if (PolicyType.READ.equals(policyType)) {
|
||||
builder.append(" {\n");
|
||||
builder.append(" \"Action\": [\n");
|
||||
builder.append(" \"s3:ListBucket\"\n");
|
||||
builder.append(" ],\n");
|
||||
builder.append(" \"Effect\": \"Deny\",\n");
|
||||
builder.append(" \"Principal\": \"*\",\n");
|
||||
builder.append(" \"Resource\": \"arn:aws:s3:::");
|
||||
builder.append(bucketName);
|
||||
builder.append("\"\n");
|
||||
builder.append(" },\n");
|
||||
}
|
||||
builder.append(" {\n");
|
||||
builder.append(" \"Action\": ");
|
||||
switch (policyType) {
|
||||
case WRITE:
|
||||
builder.append("[\n");
|
||||
builder.append(" \"s3:AbortMultipartUpload\",\n");
|
||||
builder.append(" \"s3:DeleteObject\",\n");
|
||||
builder.append(" \"s3:ListMultipartUploadParts\",\n");
|
||||
builder.append(" \"s3:PutObject\"\n");
|
||||
builder.append(" ],\n");
|
||||
break;
|
||||
case READ_WRITE:
|
||||
builder.append("[\n");
|
||||
builder.append(" \"s3:AbortMultipartUpload\",\n");
|
||||
builder.append(" \"s3:DeleteObject\",\n");
|
||||
builder.append(" \"s3:GetObject\",\n");
|
||||
builder.append(" \"s3:ListMultipartUploadParts\",\n");
|
||||
builder.append(" \"s3:PutObject\"\n");
|
||||
builder.append(" ],\n");
|
||||
break;
|
||||
default:
|
||||
builder.append("\"s3:GetObject\",\n");
|
||||
break;
|
||||
}
|
||||
builder.append(" \"Effect\": \"Allow\",\n");
|
||||
builder.append(" \"Principal\": \"*\",\n");
|
||||
builder.append(" \"Resource\": \"arn:aws:s3:::");
|
||||
builder.append(bucketName);
|
||||
builder.append("/*\"\n");
|
||||
builder.append(" }\n");
|
||||
builder.append(" ],\n");
|
||||
builder.append(" \"Version\": \"2012-10-17\"\n");
|
||||
builder.append("}\n");
|
||||
return builder.toString();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import com.qcloud.cos.ClientConfig;
|
||||
import com.qcloud.cos.auth.BasicCOSCredentials;
|
||||
import com.qcloud.cos.auth.COSCredentials;
|
||||
import com.qcloud.cos.region.Region;
|
||||
import com.ruoyi.oss.entity.UploadResult;
|
||||
import com.ruoyi.oss.enumd.CloudServiceEnumd;
|
||||
import com.ruoyi.oss.factory.OssFactory;
|
||||
import com.ruoyi.oss.properties.CloudStorageProperties;
|
||||
@@ -52,7 +53,7 @@ public class QcloudCloudStorageServiceImpl extends AbstractCloudStorageService i
|
||||
}
|
||||
|
||||
@Override
|
||||
public String upload(byte[] data, String path) {
|
||||
public UploadResult upload(byte[] data, String path) {
|
||||
// 腾讯云必需要以"/"开头
|
||||
if (!path.startsWith("/")) {
|
||||
path = "/" + path;
|
||||
@@ -64,7 +65,7 @@ public class QcloudCloudStorageServiceImpl extends AbstractCloudStorageService i
|
||||
// if (Convert.toInt(jsonObject.get("code")) != 0) {
|
||||
// throw new OssException("文件上传失败," + Convert.toStr(jsonObject.get("message")));
|
||||
// }
|
||||
return this.properties.getDomain() + path;
|
||||
return new UploadResult().setUrl(properties.getDomain() + "/" + path).setFilename(path);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -79,12 +80,12 @@ public class QcloudCloudStorageServiceImpl extends AbstractCloudStorageService i
|
||||
}
|
||||
|
||||
@Override
|
||||
public String uploadSuffix(byte[] data, String suffix) {
|
||||
public UploadResult uploadSuffix(byte[] data, String suffix) {
|
||||
return upload(data, getPath(this.properties.getPrefix(), suffix));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String uploadSuffix(InputStream inputStream, String suffix) {
|
||||
public UploadResult uploadSuffix(InputStream inputStream, String suffix) {
|
||||
return upload(inputStream, getPath(this.properties.getPrefix(), suffix));
|
||||
}
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@ import com.qiniu.storage.Configuration;
|
||||
import com.qiniu.storage.Region;
|
||||
import com.qiniu.storage.UploadManager;
|
||||
import com.qiniu.util.Auth;
|
||||
import com.ruoyi.oss.entity.UploadResult;
|
||||
import com.ruoyi.oss.enumd.CloudServiceEnumd;
|
||||
import com.ruoyi.oss.exception.OssException;
|
||||
import com.ruoyi.oss.factory.OssFactory;
|
||||
@@ -59,7 +60,7 @@ public class QiniuCloudStorageServiceImpl extends AbstractCloudStorageService im
|
||||
}
|
||||
|
||||
@Override
|
||||
public String upload(byte[] data, String path) {
|
||||
public UploadResult upload(byte[] data, String path) {
|
||||
try {
|
||||
Response res = uploadManager.put(data, path, token);
|
||||
if (!res.isOK()) {
|
||||
@@ -68,7 +69,7 @@ public class QiniuCloudStorageServiceImpl extends AbstractCloudStorageService im
|
||||
} catch (Exception e) {
|
||||
throw new OssException("上传文件失败,请核对七牛配置信息");
|
||||
}
|
||||
return this.properties.getDomain() + "/" + path;
|
||||
return new UploadResult().setUrl(properties.getDomain() + "/" + path).setFilename(path);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -85,12 +86,12 @@ public class QiniuCloudStorageServiceImpl extends AbstractCloudStorageService im
|
||||
}
|
||||
|
||||
@Override
|
||||
public String uploadSuffix(byte[] data, String suffix) {
|
||||
public UploadResult uploadSuffix(byte[] data, String suffix) {
|
||||
return upload(data, getPath(this.properties.getPrefix(), suffix));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String uploadSuffix(InputStream inputStream, String suffix) {
|
||||
public UploadResult uploadSuffix(InputStream inputStream, String suffix) {
|
||||
return upload(inputStream, getPath(this.properties.getPrefix(), suffix));
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user