cloud-etl-rule/cloud-rule-server/src/main/java/com/muyu/rule/server/DataTest.java

106 lines
3.1 KiB
Java
Raw Blame History

This file contains ambiguous Unicode 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.rule.server;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.GetObjectRequest;
import com.aliyun.oss.model.OSSObject;
import com.aliyun.oss.model.PutObjectRequest;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.NestingKind;
import javax.tools.JavaFileObject;
import java.io.*;
import javax.tools.*;
import java.net.URI;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
/**
* @Author张承志
* @Packagecom.muyu.rule.server
* @Projectcloud-etl-rule
* @nameDataTest
* @Date2024/8/27 9:43
*/
public class DataTest {
/**
* Endpoint 存储对象概述 阿里云主账号AccessKeyaccessKeySecret拥有所有API的访问权限 访问路径前缀 存储对象概述
*/
private static String endPoint = "oss-cn-beijing.aliyuncs.com";
private static String accessKeyId = "LTAI5tRRrrYqiSXddVq7RvqW";
private static String accessKeySecret = "GhEg1LlHTOx4q0rxs1S3pCaSQayCVL";
private static String accessPre = "https://zcz-vfd-1000.oss-cn-beijing.aliyuncs.com/";
private static String bucketName = "zcz-vfd-1000";
public static void main(String[] args) {
String sourceCode ="public class HelloWorld {\n" +
" public static void main(String[] args) {\n" +
" System.out.println(\"Hello, World!\");\n" +
" }\n" +
"}";
// 创建OSSClient实例。
OSS ossClient = new OSSClientBuilder().build(endPoint, accessKeyId, accessKeySecret);
// 将字符串转换为字节数组。
byte[] contentBytes = sourceCode.getBytes();
// 创建PutObjectRequest。
PutObjectRequest put = new PutObjectRequest(bucketName, "path/to/java/source/file", new ByteArrayInputStream(contentBytes));
// 上传字符串到OSS。
ossClient.putObject(put);
// 关闭OSSClient。
ossClient.shutdown();
// 创建OSSClient读取实例。
OSS ossClientReader = new OSSClientBuilder().build(endPoint,accessKeyId,accessKeySecret);
try {
GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, "test");
OSSObject ossObject = ossClientReader.getObject(getObjectRequest);
BufferedReader reader = new BufferedReader(new InputStreamReader(ossObject.getObjectContent()));
StringBuilder content = new StringBuilder();
String line=null;
while ((line = reader.readLine()) != null) {
content.append(line).append("\n");
}
System.out.println("读出的内容{}[]:"+content);
String string = content.toString();
System.out.println("==>"+string);
} catch (Exception e) {
throw new RuntimeException("读取失败:" + e.getMessage());
} finally {
// 关闭OSSClient。
ossClient.shutdown();
}
}
}