4 changed files with 522 additions and 0 deletions
@ -0,0 +1,79 @@ |
|||||
|
package org.dromara.sample.storage.config; |
||||
|
|
||||
|
import io.minio.MinioClient; |
||||
|
import org.springframework.boot.context.properties.ConfigurationProperties; |
||||
|
import org.springframework.context.annotation.Bean; |
||||
|
import org.springframework.context.annotation.Configuration; |
||||
|
|
||||
|
/** |
||||
|
* Minio 配置信息 |
||||
|
* |
||||
|
* @author ruoyi |
||||
|
*/ |
||||
|
@Configuration |
||||
|
@ConfigurationProperties(prefix = "osstianyi") |
||||
|
public class OssTianyiConfig |
||||
|
{ |
||||
|
|
||||
|
private String endPoint; |
||||
|
private String accessKey; |
||||
|
private String secretKey; |
||||
|
private String bucketName; |
||||
|
|
||||
|
public String getEndPoint() { |
||||
|
return endPoint; |
||||
|
} |
||||
|
|
||||
|
public void setEndPoint(String endPoint) { |
||||
|
this.endPoint = endPoint; |
||||
|
} |
||||
|
|
||||
|
public String getAccessKey() |
||||
|
{ |
||||
|
return accessKey; |
||||
|
} |
||||
|
|
||||
|
public void setAccessKey(String accessKey) |
||||
|
{ |
||||
|
this.accessKey = accessKey; |
||||
|
} |
||||
|
|
||||
|
public String getSecretKey() |
||||
|
{ |
||||
|
return secretKey; |
||||
|
} |
||||
|
|
||||
|
public void setSecretKey(String secretKey) |
||||
|
{ |
||||
|
this.secretKey = secretKey; |
||||
|
} |
||||
|
|
||||
|
public String getBucketName() |
||||
|
{ |
||||
|
return bucketName; |
||||
|
} |
||||
|
|
||||
|
public void setBucketName(String bucketName) |
||||
|
{ |
||||
|
this.bucketName = bucketName; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public String toString() { |
||||
|
return "OssTianyiConfig{" + |
||||
|
"endPoint='" + endPoint + '\'' + |
||||
|
", accessKey='" + accessKey + '\'' + |
||||
|
", secretKey='" + secretKey + '\'' + |
||||
|
", bucketName='" + bucketName + '\'' + |
||||
|
'}'; |
||||
|
} |
||||
|
|
||||
|
@Bean |
||||
|
public MinioClient getMinioClient() |
||||
|
{ |
||||
|
// System.out.println("OssTianyiConfig");
|
||||
|
// System.out.println("OssTianyiConfig" + endPoint);
|
||||
|
System.out.println(toString()); |
||||
|
return MinioClient.builder().endpoint(endPoint).credentials(accessKey, secretKey).build(); |
||||
|
} |
||||
|
} |
@ -0,0 +1,58 @@ |
|||||
|
package org.dromara.sample.storage.controller; |
||||
|
|
||||
|
|
||||
|
import cn.dev33.satoken.annotation.SaCheckPermission; |
||||
|
import org.dromara.common.core.domain.R; |
||||
|
import org.dromara.common.mybatis.core.page.TableDataInfo; |
||||
|
import org.dromara.common.web.core.BaseController; |
||||
|
import org.dromara.sample.storage.domain.SysUploadFile; |
||||
|
import org.dromara.sample.storage.util.OssTianyiClientUtils; |
||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||
|
import org.springframework.web.bind.annotation.*; |
||||
|
import org.springframework.web.multipart.MultipartFile; |
||||
|
|
||||
|
import java.util.HashMap; |
||||
|
|
||||
|
/** |
||||
|
* 系统上传文件Controller |
||||
|
* |
||||
|
* @author tom |
||||
|
* @date 2024-12-21 |
||||
|
*/ |
||||
|
@RestController |
||||
|
@RequestMapping("/ossTianyi") |
||||
|
public class OssTianyiController extends BaseController |
||||
|
{ |
||||
|
|
||||
|
@Autowired |
||||
|
private OssTianyiClientUtils ossTianyiClientUtils; |
||||
|
|
||||
|
/** |
||||
|
* 查询系统上传文件列表 (实际是上传接口) |
||||
|
*/ |
||||
|
@SaCheckPermission("file:sysUploadFile:list") |
||||
|
@GetMapping("/list") |
||||
|
public TableDataInfo list(SysUploadFile sysUploadFile) |
||||
|
{ |
||||
|
String localFilePath = "D:\\123pan\\page.html"; |
||||
|
try{ |
||||
|
ossTianyiClientUtils.uploadFileTest("page.html","dev/temp",localFilePath); |
||||
|
}catch (Exception e){ |
||||
|
e.printStackTrace(); |
||||
|
} |
||||
|
|
||||
|
return null; |
||||
|
} |
||||
|
|
||||
|
@GetMapping("/upload") |
||||
|
public R<HashMap<String, Object>> upload(@RequestPart("file") MultipartFile file) { |
||||
|
try { |
||||
|
ossTianyiClientUtils.uploadFile("page.html", "dev/temp", file); |
||||
|
} catch (Exception e) { |
||||
|
e.printStackTrace(); |
||||
|
} |
||||
|
|
||||
|
return null; |
||||
|
} |
||||
|
|
||||
|
} |
@ -0,0 +1,211 @@ |
|||||
|
package org.dromara.sample.storage.domain; |
||||
|
|
||||
|
import com.baomidou.mybatisplus.annotation.IdType; |
||||
|
import com.baomidou.mybatisplus.annotation.TableField; |
||||
|
import com.baomidou.mybatisplus.annotation.TableId; |
||||
|
import com.baomidou.mybatisplus.annotation.TableName; |
||||
|
import org.apache.commons.lang3.builder.ToStringBuilder; |
||||
|
import org.apache.commons.lang3.builder.ToStringStyle; |
||||
|
import org.dromara.common.mybatis.core.domain.BaseEntity; |
||||
|
|
||||
|
/** |
||||
|
* 系统上传文件对象 sys_upload_file |
||||
|
* |
||||
|
* @author tom |
||||
|
* @date 2024-12-21 |
||||
|
*/ |
||||
|
@TableName("sys_upload_file") |
||||
|
public class SysUploadFile extends BaseEntity |
||||
|
{ |
||||
|
private static final long serialVersionUID = 1L; |
||||
|
|
||||
|
/** ID */ |
||||
|
@TableId(value="id",type = IdType.AUTO) |
||||
|
private Long id; |
||||
|
|
||||
|
/** 删除标记 */ |
||||
|
@TableField("del_flag") |
||||
|
private Long delFlag; |
||||
|
|
||||
|
/** 文件名称 */ |
||||
|
@TableField("name") |
||||
|
private String name; |
||||
|
|
||||
|
/** 上传时文件名 */ |
||||
|
@TableField("file_name") |
||||
|
private String fileName; |
||||
|
|
||||
|
/** 物理路径 */ |
||||
|
@TableField("path") |
||||
|
private String path; |
||||
|
|
||||
|
/** 文件URL */ |
||||
|
@TableField("url") |
||||
|
private String url; |
||||
|
|
||||
|
/** 文件扩展名 */ |
||||
|
@TableField("extend") |
||||
|
private String extend; |
||||
|
|
||||
|
/** 文件类型 */ |
||||
|
@TableField("dict_file_type") |
||||
|
private Long dictFileType; |
||||
|
|
||||
|
/** 关联对象ID */ |
||||
|
@TableField("object_id") |
||||
|
private Long objectId; |
||||
|
|
||||
|
/** 关联对象类型 */ |
||||
|
@TableField("object_type") |
||||
|
private String objectType; |
||||
|
|
||||
|
/** 关联对象ID */ |
||||
|
@TableField("object_link_id") |
||||
|
private String objectLinkId; |
||||
|
|
||||
|
/** 排序 */ |
||||
|
@TableField("sort") |
||||
|
private Long sort; |
||||
|
|
||||
|
/** 标签 */ |
||||
|
@TableField("tags") |
||||
|
private String tags; |
||||
|
|
||||
|
public void setId(Long id) |
||||
|
{ |
||||
|
this.id = id; |
||||
|
} |
||||
|
|
||||
|
public Long getId() |
||||
|
{ |
||||
|
return id; |
||||
|
} |
||||
|
public void setDelFlag(Long delFlag) |
||||
|
{ |
||||
|
this.delFlag = delFlag; |
||||
|
} |
||||
|
|
||||
|
public Long getDelFlag() |
||||
|
{ |
||||
|
return delFlag; |
||||
|
} |
||||
|
public void setName(String name) |
||||
|
{ |
||||
|
this.name = name; |
||||
|
} |
||||
|
|
||||
|
public String getName() |
||||
|
{ |
||||
|
return name; |
||||
|
} |
||||
|
public void setFileName(String fileName) |
||||
|
{ |
||||
|
this.fileName = fileName; |
||||
|
} |
||||
|
|
||||
|
public String getFileName() |
||||
|
{ |
||||
|
return fileName; |
||||
|
} |
||||
|
public void setPath(String path) |
||||
|
{ |
||||
|
this.path = path; |
||||
|
} |
||||
|
|
||||
|
public String getPath() |
||||
|
{ |
||||
|
return path; |
||||
|
} |
||||
|
public void setUrl(String url) |
||||
|
{ |
||||
|
this.url = url; |
||||
|
} |
||||
|
|
||||
|
public String getUrl() |
||||
|
{ |
||||
|
return url; |
||||
|
} |
||||
|
public void setExtend(String extend) |
||||
|
{ |
||||
|
this.extend = extend; |
||||
|
} |
||||
|
|
||||
|
public String getExtend() |
||||
|
{ |
||||
|
return extend; |
||||
|
} |
||||
|
public void setDictFileType(Long dictFileType) |
||||
|
{ |
||||
|
this.dictFileType = dictFileType; |
||||
|
} |
||||
|
|
||||
|
public Long getDictFileType() |
||||
|
{ |
||||
|
return dictFileType; |
||||
|
} |
||||
|
public void setObjectId(Long objectId) |
||||
|
{ |
||||
|
this.objectId = objectId; |
||||
|
} |
||||
|
|
||||
|
public Long getObjectId() |
||||
|
{ |
||||
|
return objectId; |
||||
|
} |
||||
|
public void setObjectType(String objectType) |
||||
|
{ |
||||
|
this.objectType = objectType; |
||||
|
} |
||||
|
|
||||
|
public String getObjectType() |
||||
|
{ |
||||
|
return objectType; |
||||
|
} |
||||
|
|
||||
|
public String getObjectLinkId() { |
||||
|
return objectLinkId; |
||||
|
} |
||||
|
|
||||
|
public void setObjectLinkId(String objectLinkId) { |
||||
|
this.objectLinkId = objectLinkId; |
||||
|
} |
||||
|
|
||||
|
public void setSort(Long sort) |
||||
|
{ |
||||
|
this.sort = sort; |
||||
|
} |
||||
|
|
||||
|
public Long getSort() |
||||
|
{ |
||||
|
return sort; |
||||
|
} |
||||
|
|
||||
|
public String getTags() { |
||||
|
return tags; |
||||
|
} |
||||
|
|
||||
|
public void setTags(String tags) { |
||||
|
this.tags = tags; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public String toString() { |
||||
|
return new ToStringBuilder(this,ToStringStyle.MULTI_LINE_STYLE) |
||||
|
.append("id", getId()) |
||||
|
.append("createBy", getCreateBy()) |
||||
|
.append("createTime", getCreateTime()) |
||||
|
.append("updateBy", getUpdateBy()) |
||||
|
.append("updateTime", getUpdateTime()) |
||||
|
.append("delFlag", getDelFlag()) |
||||
|
.append("name", getName()) |
||||
|
.append("fileName", getFileName()) |
||||
|
.append("path", getPath()) |
||||
|
.append("url", getUrl()) |
||||
|
.append("extend", getExtend()) |
||||
|
.append("dictFileType", getDictFileType()) |
||||
|
.append("objectId", getObjectId()) |
||||
|
.append("objectType", getObjectType()) |
||||
|
.append("sort", getSort()) |
||||
|
.toString(); |
||||
|
} |
||||
|
} |
@ -0,0 +1,174 @@ |
|||||
|
package org.dromara.sample.storage.util; |
||||
|
|
||||
|
import cn.hutool.core.io.FileUtil; |
||||
|
import io.minio.*; |
||||
|
import io.minio.errors.*; |
||||
|
import io.minio.messages.Item; |
||||
|
import org.dromara.sample.storage.config.OssTianyiConfig; |
||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||
|
import org.springframework.stereotype.Component; |
||||
|
import org.springframework.web.multipart.MultipartFile; |
||||
|
|
||||
|
import java.io.BufferedInputStream; |
||||
|
import java.io.File; |
||||
|
import java.io.IOException; |
||||
|
import java.io.InputStream; |
||||
|
import java.nio.file.Files; |
||||
|
import java.security.InvalidKeyException; |
||||
|
import java.security.NoSuchAlgorithmException; |
||||
|
|
||||
|
@Component |
||||
|
public class OssTianyiClientUtils { |
||||
|
|
||||
|
private static MinioClient minioClient; |
||||
|
|
||||
|
// private static String endPoint = "https://jiangsu-10.zos.ctyun.cn"; //地域节点,可访问
|
||||
|
// // private static String endPoint = "https://dk-ty-oss-bucket.jiangsu-10.zos.ctyun.cn"; //说是桶的域名地址,支持公网访问ZOS,实测报错
|
||||
|
// private static String minioAccessKey = "22e3f37368a242b38f4f25d98c9baf1f";
|
||||
|
// private static String minioSecretKey = "0d858850666248e59830d9a596847062";
|
||||
|
|
||||
|
// private static String bucketName = "dk-ty-oss-bucket";
|
||||
|
// private String bucketName = "dkossbucket";
|
||||
|
private String objectName = "page.html"; |
||||
|
//注意,天翼云里默认路径就是文件名,不需要在前面加根路径/
|
||||
|
private String filePath = "dev/temp/"; |
||||
|
private String fileDir = "dev"; |
||||
|
|
||||
|
|
||||
|
@Autowired |
||||
|
OssTianyiConfig ossTianyiConfig; |
||||
|
|
||||
|
@Autowired |
||||
|
public OssTianyiClientUtils(OssTianyiConfig ossTianyiConfig) { |
||||
|
this.ossTianyiConfig = ossTianyiConfig; |
||||
|
init(); |
||||
|
} |
||||
|
|
||||
|
private void init() { |
||||
|
this.minioClient = MinioClient.builder() |
||||
|
.endpoint(ossTianyiConfig.getEndPoint()) |
||||
|
.credentials(ossTianyiConfig.getAccessKey(), ossTianyiConfig.getSecretKey()) |
||||
|
.build(); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 天翼云OSS初始化(采用S3 Compatible Storage) |
||||
|
*/ |
||||
|
// public void tianyiOssInit() {
|
||||
|
// minioClient = MinioClient.builder()
|
||||
|
// .endpoint(ossTianyiConfig.getEndPoint())
|
||||
|
// .credentials(ossTianyiConfig.getAccessKey(), ossTianyiConfig.getSecretKey())
|
||||
|
// .build();
|
||||
|
// }
|
||||
|
|
||||
|
/** |
||||
|
* 文件上传 (固定) |
||||
|
*/ |
||||
|
public void uploadFileTest(String objectName, String filePath, String localFilePath) throws Exception { |
||||
|
File file = new File(localFilePath); |
||||
|
BufferedInputStream inputStream = FileUtil.getInputStream(file); |
||||
|
String contentType = Files.probeContentType(file.toPath()); |
||||
|
if (contentType == null) { |
||||
|
contentType = "application/octet-stream"; |
||||
|
} |
||||
|
PutObjectArgs args = PutObjectArgs.builder() |
||||
|
.bucket(ossTianyiConfig.getBucketName()) |
||||
|
.object(filePath + objectName) |
||||
|
.stream(inputStream, FileUtil.size(file), -1) |
||||
|
.contentType(contentType) |
||||
|
.build(); |
||||
|
minioClient.putObject(args); |
||||
|
} |
||||
|
|
||||
|
public void uploadFile(String s, String s1, MultipartFile file) { |
||||
|
try { |
||||
|
PutObjectArgs args = PutObjectArgs.builder() |
||||
|
.bucket(ossTianyiConfig.getBucketName()) |
||||
|
.object( file.getName()) |
||||
|
.stream(file.getInputStream(), file.getSize(), -1) |
||||
|
.contentType(file.getContentType()) |
||||
|
.build(); |
||||
|
minioClient.putObject(args); |
||||
|
} catch (Exception e) { |
||||
|
e.printStackTrace(); |
||||
|
} |
||||
|
|
||||
|
} |
||||
|
|
||||
|
|
||||
|
|
||||
|
/** |
||||
|
* 文件删除 |
||||
|
*/ |
||||
|
public void deleteFile(String bucketName, String objectName) throws Exception { |
||||
|
minioClient.removeObject( |
||||
|
RemoveObjectArgs.builder() |
||||
|
.bucket(bucketName) |
||||
|
.object(objectName) |
||||
|
.build()); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 文件下载 |
||||
|
*/ |
||||
|
public void downloadFile(String bucketName, String objectName, String downloadPath) throws Exception { |
||||
|
InputStream inputStream = minioClient.getObject( |
||||
|
GetObjectArgs.builder() |
||||
|
.bucket(bucketName) |
||||
|
.object(objectName) |
||||
|
.build() |
||||
|
); |
||||
|
FileUtil.writeFromStream(inputStream, new File(downloadPath)); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 文件列表 |
||||
|
* @param bucketName |
||||
|
* @param prefix |
||||
|
*/ |
||||
|
public void listFiles(String bucketName, String prefix) { |
||||
|
Iterable<Result<Item>> results = minioClient.listObjects( |
||||
|
ListObjectsArgs.builder() |
||||
|
.bucket(bucketName) |
||||
|
.prefix(prefix) |
||||
|
.recursive(true) |
||||
|
.build() |
||||
|
); |
||||
|
|
||||
|
try { |
||||
|
for (Result<Item> result : results) { |
||||
|
try { |
||||
|
// System.out.println("文件名:" + result.get().objectName()); // 获取对象名称(文件路径)
|
||||
|
System.out.println(result.get().objectName()); // 获取对象名称(文件路径)
|
||||
|
} catch (Exception e) { |
||||
|
e.printStackTrace(); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
} catch (Exception e) { |
||||
|
System.err.println("文件读取异常: " + e.getMessage()); |
||||
|
} |
||||
|
|
||||
|
} |
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
// public void updateFile(String bucketName, String objectName, String filePath, String localFilePath) throws Exception {
|
||||
|
// uploadFile(bucketName, objectName, filePath, localFilePath);
|
||||
|
// }
|
||||
|
|
||||
|
|
||||
|
// public static void main(String[] args) throws Exception {
|
||||
|
// String localFilePath = "D:\\123pan\\page.html";
|
||||
|
//
|
||||
|
// tianyiOssInit("", "", "");
|
||||
|
// uploadFile(bucketName, objectName, filePath, localFilePath);//正常
|
||||
|
//// deleteFile(bucketName, filePath + objectName);//正常
|
||||
|
//// listFiles(bucketName, fileDir + "/"); // 正常
|
||||
|
//
|
||||
|
//// downloadFile(bucketName, filePath + objectName, "D:\\123pan\\downloaded_page.html"); //正常
|
||||
|
//
|
||||
|
// }
|
||||
|
|
||||
|
} |
Loading…
Reference in new issue