70 lines
2.6 KiB
Java
70 lines
2.6 KiB
Java
package com.muyu.load;
|
||
|
||
import com.aliyun.oss.OSSClient;
|
||
import com.aliyun.oss.model.GetObjectRequest;
|
||
import com.aliyun.oss.model.OSSObject;
|
||
import com.aliyun.oss.model.ObjectMetadata;
|
||
import com.muyu.common.core.domain.Result;
|
||
import com.muyu.compile.OSSFileCompile;
|
||
|
||
import java.io.BufferedReader;
|
||
import java.io.File;
|
||
import java.io.IOException;
|
||
import java.io.InputStreamReader;
|
||
import java.util.Map;
|
||
|
||
/**
|
||
* @Author:蓬叁
|
||
* @Package:com.muyu.load
|
||
* @Project:cloud-rule
|
||
* @name:OSSFileLoad
|
||
* @Date:2024/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";
|
||
// 本地文件下载路径
|
||
private static final String localpath = "C:\\Users\\13636\\Desktop\\FileLoad\\";
|
||
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("预下载文件不存在");
|
||
}
|
||
ossClient.getObject(new GetObjectRequest(bucketName, fileName), new File(localpath));
|
||
// 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();
|
||
ossClient.shutdown();
|
||
|
||
// Map<String, byte[]> compile = OSSFileCompile.compile(fileName, source, "target/");
|
||
Result<Object> compile = OSSFileCompile.compile(localpath+fileName, null);
|
||
System.out.println(compile.getData());
|
||
return Result.success(source);
|
||
}
|
||
|
||
}
|