--9--10
parent
23bc06a35e
commit
ddbce1b005
|
@ -1,77 +1,77 @@
|
||||||
package com.muyu.utils;
|
//package com.muyu.utils;
|
||||||
|
//
|
||||||
import com.aliyun.oss.OSS;
|
//import com.aliyun.oss.OSS;
|
||||||
import com.aliyun.oss.OSSClientBuilder;
|
//import com.aliyun.oss.OSSClientBuilder;
|
||||||
import com.aliyun.oss.model.GetObjectRequest;
|
//import com.aliyun.oss.model.GetObjectRequest;
|
||||||
import com.aliyun.oss.model.OSSObject;
|
//import com.aliyun.oss.model.OSSObject;
|
||||||
import com.aliyun.oss.model.ObjectListing;
|
//import com.aliyun.oss.model.ObjectListing;
|
||||||
import com.aliyun.oss.model.OSSObjectSummary;
|
//import com.aliyun.oss.model.OSSObjectSummary;
|
||||||
import lombok.extern.log4j.Log4j2;
|
//import lombok.extern.log4j.Log4j2;
|
||||||
import org.springframework.boot.ApplicationArguments;
|
//import org.springframework.boot.ApplicationArguments;
|
||||||
import org.springframework.boot.ApplicationRunner;
|
//import org.springframework.boot.ApplicationRunner;
|
||||||
import org.springframework.stereotype.Component;
|
//import org.springframework.stereotype.Component;
|
||||||
|
//
|
||||||
import java.io.File;
|
//import java.io.File;
|
||||||
import java.io.FileOutputStream;
|
//import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
//import java.io.IOException;
|
||||||
|
//
|
||||||
@Log4j2
|
//@Log4j2
|
||||||
@Component
|
//@Component
|
||||||
public class DownloadOssSynchronization implements ApplicationRunner {
|
//public class DownloadOssSynchronization implements ApplicationRunner {
|
||||||
|
//
|
||||||
private static final String endpoint = "https://oss-cn-shanghai.aliyuncs.com";
|
// private static final String endpoint = "https://oss-cn-shanghai.aliyuncs.com";
|
||||||
|
//
|
||||||
private static final String accessKeyId = "LTAI5t8LmMHfW8ckPaAZR6oQ";
|
// private static final String accessKeyId = "LTAI5t8LmMHfW8ckPaAZR6oQ";
|
||||||
|
//
|
||||||
private static final String accessKeySecret = "pwzEwkpxcTFgurkARyr7sG7V6syc9x";
|
// private static final String accessKeySecret = "pwzEwkpxcTFgurkARyr7sG7V6syc9x";
|
||||||
|
//
|
||||||
private static final String bucketName = "011811";
|
// private static final String bucketName = "011811";
|
||||||
|
//
|
||||||
/**
|
// /**
|
||||||
* 服务器项目路径放文件
|
// * 服务器项目路径放文件
|
||||||
*/
|
// */
|
||||||
private static String serverClassPath = "home/lib/com/muyu/abstracts/";
|
// private static String serverClassPath = "home/lib/com/muyu/abstracts/";
|
||||||
|
//
|
||||||
|
//
|
||||||
@Override
|
// @Override
|
||||||
public void run(ApplicationArguments args) throws Exception {
|
// public void run(ApplicationArguments args) throws Exception {
|
||||||
log.info("开始从Oss下载规则引擎");
|
// log.info("开始从Oss下载规则引擎");
|
||||||
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
|
// OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
|
||||||
|
//
|
||||||
// 获取存储桶信息
|
// // 获取存储桶信息
|
||||||
ObjectListing objectListing = ossClient.listObjects(bucketName);
|
// ObjectListing objectListing = ossClient.listObjects(bucketName);
|
||||||
// 循环获取Oss存储桶中的所有对象
|
// // 循环获取Oss存储桶中的所有对象
|
||||||
for (OSSObjectSummary objectSummary : objectListing.getObjectSummaries()) {
|
// for (OSSObjectSummary objectSummary : objectListing.getObjectSummaries()) {
|
||||||
String key = objectSummary.getKey();
|
// String key = objectSummary.getKey();
|
||||||
downloadFile(ossClient, bucketName, key, serverClassPath);
|
// downloadFile(ossClient, bucketName, key, serverClassPath);
|
||||||
}
|
// }
|
||||||
// 关闭OSSClient
|
// // 关闭OSSClient
|
||||||
ossClient.shutdown();
|
// ossClient.shutdown();
|
||||||
//下载完成实现批量批量类加载
|
// //下载完成实现批量批量类加载
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
private void downloadFile(OSS ossClient, String bucketName, String key, String serverPath) {
|
// private void downloadFile(OSS ossClient, String bucketName, String key, String serverPath) {
|
||||||
log.info("开始下载文件[]{}:"+key);
|
// log.info("开始下载文件[]{}:"+key);
|
||||||
//创建服务器项目容器的文件目录路径
|
// //创建服务器项目容器的文件目录路径
|
||||||
File serverFile = new File(serverPath, key);
|
// File serverFile = new File(serverPath, key);
|
||||||
//如果目录不存在,则创建目录
|
// //如果目录不存在,则创建目录
|
||||||
if (!serverFile.getParentFile().exists()) {
|
// if (!serverFile.getParentFile().exists()) {
|
||||||
serverFile.getParentFile().mkdirs();
|
// serverFile.getParentFile().mkdirs();
|
||||||
}
|
// }
|
||||||
//下载文件到项目中
|
// //下载文件到项目中
|
||||||
OSSObject ossObject = ossClient.getObject(new GetObjectRequest(bucketName, key));
|
// OSSObject ossObject = ossClient.getObject(new GetObjectRequest(bucketName, key));
|
||||||
|
//
|
||||||
try (FileOutputStream fos = new FileOutputStream(serverFile);) {
|
// try (FileOutputStream fos = new FileOutputStream(serverFile);) {
|
||||||
byte[] buffer = new byte[1024];
|
// byte[] buffer = new byte[1024];
|
||||||
int byteRead;
|
// int byteRead;
|
||||||
while ((byteRead = ossObject.getObjectContent().read(buffer)) != -1) {
|
// while ((byteRead = ossObject.getObjectContent().read(buffer)) != -1) {
|
||||||
fos.write(buffer, 0, byteRead);
|
// fos.write(buffer, 0, byteRead);
|
||||||
}
|
// }
|
||||||
log.info("文件 " + key + "已经下载到" + serverFile.getAbsolutePath());
|
// log.info("文件 " + key + "已经下载到" + serverFile.getAbsolutePath());
|
||||||
} catch (IOException e) {
|
// } catch (IOException e) {
|
||||||
log.error("下载文件失败:" + key);
|
// log.error("下载文件失败:" + key);
|
||||||
e.printStackTrace();
|
// e.printStackTrace();
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
}
|
//}
|
||||||
|
|
|
@ -1,91 +1,91 @@
|
||||||
package com.muyu.utils;
|
//package com.muyu.utils;
|
||||||
|
//
|
||||||
import com.aliyun.oss.OSS;
|
//import com.aliyun.oss.OSS;
|
||||||
import com.aliyun.oss.OSSClientBuilder;
|
//import com.aliyun.oss.OSSClientBuilder;
|
||||||
import com.aliyun.oss.model.PutObjectRequest;
|
//import com.aliyun.oss.model.PutObjectRequest;
|
||||||
import com.muyu.common.core.domain.Result;
|
//import com.muyu.common.core.domain.Result;
|
||||||
|
//
|
||||||
import java.io.ByteArrayInputStream;
|
//import java.io.ByteArrayInputStream;
|
||||||
import java.io.File;
|
//import java.io.File;
|
||||||
import java.nio.charset.StandardCharsets;
|
//import java.nio.charset.StandardCharsets;
|
||||||
|
//
|
||||||
/**
|
///**
|
||||||
* @Author:LiDongJia
|
// * @Author:LiDongJia
|
||||||
* @Package:com.muyu.cloud.etl.rule.upload
|
// * @Package:com.muyu.cloud.etl.rule.upload
|
||||||
* @Project:cloud-rule
|
// * @Project:cloud-rule
|
||||||
* @name:ALiYunUpload
|
// * @name:ALiYunUpload
|
||||||
* @Date:2024/8/30
|
// * @Date:2024/8/30
|
||||||
*/
|
// */
|
||||||
public class OssUpload {
|
//public class OssUpload {
|
||||||
|
//
|
||||||
// Endpoint以华东1(杭州)为例,其它Region请按实际情况填写
|
// // Endpoint以华东1(杭州)为例,其它Region请按实际情况填写
|
||||||
private static final String endpoint = "https://oss-cn-shanghai.aliyuncs.com";
|
// private static final String endpoint = "https://oss-cn-shanghai.aliyuncs.com";
|
||||||
|
//
|
||||||
private static final String accessKeyId = "LTAI5t8LmMHfW8ckPaAZR6oQ";
|
// private static final String accessKeyId = "LTAI5t8LmMHfW8ckPaAZR6oQ";
|
||||||
|
//
|
||||||
private static final String accessKeySecret = "pwzEwkpxcTFgurkARyr7sG7V6syc9x";
|
// private static final String accessKeySecret = "pwzEwkpxcTFgurkARyr7sG7V6syc9x";
|
||||||
|
//
|
||||||
private static final String bucketName = "011811";
|
// private static final String bucketName = "011811";
|
||||||
// Bucket名称
|
// // Bucket名称
|
||||||
|
//
|
||||||
public static Result<Object> uploadFiles(String content, String fileName) {
|
// public static Result<Object> uploadFiles(String content, String fileName) {
|
||||||
|
//
|
||||||
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
|
// OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
|
||||||
|
//
|
||||||
// 上传到OSS后文件保存的目录,例如:folder/subfolder/,最后以斜杠结尾
|
// // 上传到OSS后文件保存的目录,例如:folder/subfolder/,最后以斜杠结尾
|
||||||
String objectName = "build/rule/version/" + fileName + ".java";
|
// String objectName = "build/rule/version/" + fileName + ".java";
|
||||||
|
//
|
||||||
try {
|
// try {
|
||||||
// 将内容转换为字节数组输入流
|
// // 将内容转换为字节数组输入流
|
||||||
ByteArrayInputStream inputStream = new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8));
|
// ByteArrayInputStream inputStream = new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8));
|
||||||
|
//
|
||||||
// 构造上传请求
|
// // 构造上传请求
|
||||||
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectName, inputStream);
|
// PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectName, inputStream);
|
||||||
|
//
|
||||||
// 执行上传操作
|
// // 执行上传操作
|
||||||
// ossClient.putObject(putObjectRequest);
|
//// ossClient.putObject(putObjectRequest);
|
||||||
|
//
|
||||||
return Result.success(null, "文件" + fileName + ".java" + "存储成功");
|
// return Result.success(null, "文件" + fileName + ".java" + "存储成功");
|
||||||
|
//
|
||||||
} catch (Exception e) {
|
// } catch (Exception e) {
|
||||||
e.printStackTrace();
|
// e.printStackTrace();
|
||||||
|
//
|
||||||
return Result.error(null, "文件" + fileName + ".java" + "存储失败");
|
// return Result.error(null, "文件" + fileName + ".java" + "存储失败");
|
||||||
} finally {
|
// } finally {
|
||||||
// 关闭OSSClient
|
// // 关闭OSSClient
|
||||||
if (ossClient != null) {
|
// if (ossClient != null) {
|
||||||
ossClient.shutdown();
|
// ossClient.shutdown();
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
public static Result<Object> uploadClassFiles(String versionClass) {
|
// public static Result<Object> uploadClassFiles(String versionClass) {
|
||||||
// 创建OSSClient实例。
|
// // 创建OSSClient实例。
|
||||||
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
|
// OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
|
||||||
|
//
|
||||||
try {
|
// try {
|
||||||
// 填写Object完整路径,例如exampledir/exampleobject.txt。Object完整路径中不能包含Bucket名称。
|
// // 填写Object完整路径,例如exampledir/exampleobject.txt。Object完整路径中不能包含Bucket名称。
|
||||||
String objectName = "build/rule/version/" + versionClass + ".class";
|
// String objectName = "build/rule/version/" + versionClass + ".class";
|
||||||
|
//
|
||||||
// 创建上传文件的输入流(这里以文件为例)。
|
// // 创建上传文件的输入流(这里以文件为例)。
|
||||||
File file = new File("/home/lib/com/muyu/cloud/rule/engine/" + versionClass + ".class");
|
// File file = new File("/home/lib/com/muyu/cloud/rule/engine/" + versionClass + ".class");
|
||||||
|
//
|
||||||
// 上传文件。
|
// // 上传文件。
|
||||||
ossClient.putObject(new PutObjectRequest(bucketName, objectName, file));
|
// ossClient.putObject(new PutObjectRequest(bucketName, objectName, file));
|
||||||
|
//
|
||||||
System.out.println("上传成功");
|
// System.out.println("上传成功");
|
||||||
return Result.success("上传成功");
|
// return Result.success("上传成功");
|
||||||
} catch (Exception e) {
|
// } catch (Exception e) {
|
||||||
e.printStackTrace();
|
// e.printStackTrace();
|
||||||
System.out.println("上传失败");
|
// System.out.println("上传失败");
|
||||||
return Result.error("上传失败");
|
// return Result.error("上传失败");
|
||||||
} finally {
|
// } finally {
|
||||||
// 关闭OSSClient。
|
// // 关闭OSSClient。
|
||||||
if (ossClient != null) {
|
// if (ossClient != null) {
|
||||||
ossClient.shutdown();
|
// ossClient.shutdown();
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
}
|
//}
|
||||||
|
|
Loading…
Reference in New Issue