cloud-rule/cloud-rule-engine/src/main/java/com/muyu/load/OSSFileLoad.java

71 lines
2.6 KiB
Java
Raw Blame History

This file contains invisible Unicode characters!

This file contains invisible Unicode characters that may be processed differently from what appears below. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to reveal hidden characters.

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package com.muyu.load;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.OSSObject;
import com.muyu.common.core.domain.Result;
import com.muyu.compile.OSSFileCompile;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Map;
/**
* @Author蓬叁
* @Packagecom.muyu.load
* @Projectcloud-rule
* @nameOSSFileLoad
* @Date2024/8/31 下午6:38
*/
public class OSSFileLoad {
// 阿里云 endpoint
private static final String endpoint = "oss-cn-shanghai.aliyuncs.com";
// 阿里云 accessKeyId
private static final String accessKeyId = "LTAI5tHKdDATVKeBjFH8mb8D";
// 阿里云 accessKeySecret  
private static final String accessKeySecret = "5ejb4qdgukfD1FdM181kdeAkSuURo7";
// bucket
private static final String bucketName = "java-web-generating-class";
public static Result<Object> streamingDownload(String fileName) throws IOException {
String source = null;
// 创建 OSSClient 实例
OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
// 判断 bucket 是否存在
Boolean flag_bucket = ossClient.doesBucketExist(bucketName);
if (!flag_bucket) {
System.out.println("bucket不存在");
return Result.error("bucket不存在");
}
// 判断 要下载的文件 是否存在
Boolean flag_file = ossClient.doesObjectExist(bucketName, fileName);
if (!flag_file) {
System.out.println("预下载文件不存在");
return Result.error("预下载文件不存在");
}
// ossObject包含文件所在的存储空间名称、文件名称、文件元信息以及一个输入流
OSSObject ossObject = ossClient.getObject(bucketName, fileName);
// 按行读取文件内容
BufferedReader reader = new BufferedReader(new InputStreamReader(ossObject.getObjectContent()));
while(true){
String line = reader.readLine();
source += line;
if (line == null) break;
}
System.out.println(source);
System.out.println("读取成功");
// 数据读取完成后,获取的流必须关闭,否则会造成连接泄漏,导致请求无连接可用,程序无法正常工作
reader.close();
// 关闭oss
ossClient.shutdown();
// Map<String, byte[]> compile = OSSFileCompile.compile(fileName, source, "target/");
Result<Object> compile = OSSFileCompile.compile(source, "target/");
System.out.println(compile.getData());
return Result.success(source);
}
}