5 changed files with 6 additions and 315 deletions
@ -1,79 +0,0 @@ |
|||||
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(); |
|
||||
} |
|
||||
} |
|
@ -1,58 +0,0 @@ |
|||||
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; |
|
||||
} |
|
||||
|
|
||||
} |
|
@ -1,174 +0,0 @@ |
|||||
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