Oss上传测试类
parent
a907cbf5b3
commit
951248de6c
|
@ -0,0 +1,164 @@
|
|||
//package com.muyu.utils;
|
||||
//
|
||||
//import lombok.extern.log4j.Log4j2;
|
||||
//import org.springframework.web.multipart.MultipartFile;
|
||||
//
|
||||
//import java.io.*;
|
||||
//import java.time.LocalDateTime;
|
||||
//import java.util.UUID;
|
||||
//
|
||||
///**
|
||||
// * Oss服务调用
|
||||
// */
|
||||
//@Log4j2
|
||||
//public class OssUtil {
|
||||
//
|
||||
// /**
|
||||
// * Endpoint 存储对象概述 阿里云主账号AccessKey,accessKeySecret拥有所有API的访问权限 访问路径前缀 存储对象概述
|
||||
// */
|
||||
// private static String endPoint = "oss-cn-beijing.aliyuncs.com";
|
||||
// private static String accessKeyId = " LTAI5tFNfBpzEbLkntksCgwC";
|
||||
// private static String accessKeySecret = "XmYwUEea6BpgssdLKXMlpdzCI42Pk7";
|
||||
// private static String accessPre = "https://qdm123.oss-cn-beijing.aliyuncs.com/";
|
||||
//
|
||||
// /**
|
||||
// * bucket名称
|
||||
// *
|
||||
// * @return
|
||||
// */
|
||||
// private static String bucketName = "qdm123";
|
||||
//
|
||||
// private static Oss ossClient;
|
||||
//
|
||||
// static {
|
||||
// ossClient = new OSSClientBuilder().build(
|
||||
// endPoint,
|
||||
// accessKeyId,
|
||||
// accessKeySecret);
|
||||
// log.info("oss服务连接成功!");
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * 默认路径上传本地文件
|
||||
// *
|
||||
// * @param filePath
|
||||
// */
|
||||
// public static String uploadFile(String filePath) {
|
||||
// return uploadFileForBucket(bucketName, getOssFilePath(filePath), filePath);
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * 默认路径上传multipartFile文件
|
||||
// *
|
||||
// * @param multipartFile
|
||||
// */
|
||||
// public static String uploadMultipartFile(MultipartFile multipartFile) {
|
||||
// return uploadMultipartFile(bucketName, getOssFilePath(multipartFile.getOriginalFilename()), multipartFile);
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * 上传 multipartFile 类型文件
|
||||
// *
|
||||
// * @param bucketName
|
||||
// * @param ossPath
|
||||
// * @param multipartFile
|
||||
// */
|
||||
// public static String uploadMultipartFile(String bucketName, String ossPath, MultipartFile multipartFile) {
|
||||
// InputStream inputStream = null;
|
||||
// try {
|
||||
// inputStream = multipartFile.getInputStream();
|
||||
// } catch (IOException e) {
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
// uploadFileInputStreamForBucket(bucketName, ossPath, inputStream);
|
||||
// return accessPre + ossPath;
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * 使用File上传PutObject上传文件 ** 程序默认使用次方法上传
|
||||
// *
|
||||
// * @param bucketName 实例名称
|
||||
// * @param ossPath oss存储路径
|
||||
// * @param filePath 本地文件路径
|
||||
// */
|
||||
// public static String uploadFileForBucket(String bucketName, String ossPath, String filePath) {
|
||||
// // 创建PutObjectRequest对象。
|
||||
// PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, ossPath, new File(filePath));
|
||||
//
|
||||
// // 上传
|
||||
// ossClient.putObject(putObjectRequest);
|
||||
// return accessPre + ossPath;
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * 使用文件流上传到指定的bucket实例
|
||||
// *
|
||||
// * @param bucketName 实例名称
|
||||
// * @param ossPath oss存储路径
|
||||
// * @param filePath 本地文件路径
|
||||
// */
|
||||
// public static String uploadFileInputStreamForBucket(String bucketName, String ossPath, String filePath) {
|
||||
//
|
||||
// // 填写本地文件的完整路径。如果未指定本地路径,则默认从示例程序所属项目对应本地路径中上传文件流。
|
||||
// InputStream inputStream = null;
|
||||
// try {
|
||||
// inputStream = new FileInputStream(filePath);
|
||||
// } catch (FileNotFoundException e) {
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
// // 填写Bucket名称和Object完整路径。Object完整路径中不能包含Bucket名称。
|
||||
// uploadFileInputStreamForBucket(bucketName, ossPath, inputStream);
|
||||
// return accessPre + ossPath;
|
||||
// }
|
||||
//
|
||||
// public static void uploadFileInputStreamForBucket(String bucketName, String ossPath, InputStream inputStream) {
|
||||
// ossClient.putObject(bucketName, ossPath, inputStream);
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * 下载
|
||||
// *
|
||||
// * @param ossFilePath
|
||||
// * @param filePath
|
||||
// */
|
||||
// public static void downloadFile(String ossFilePath, String filePath) {
|
||||
// downloadFileForBucket(bucketName, ossFilePath, filePath);
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * 下载
|
||||
// *
|
||||
// * @param bucketName 实例名称
|
||||
// * @param ossFilePath oss存储路径
|
||||
// * @param filePath 本地文件路径
|
||||
// */
|
||||
// public static void downloadFileForBucket(String bucketName, String ossFilePath, String filePath) {
|
||||
// ossClient.getObject(new GetObjectRequest(bucketName, ossFilePath), new File(filePath));
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * @return
|
||||
// */
|
||||
// public static String getOssDefaultPath() {
|
||||
// LocalDateTime now = LocalDateTime.now();
|
||||
// String url =
|
||||
// now.getYear() + "/" +
|
||||
// now.getMonth() + "/" +
|
||||
// now.getDayOfMonth() + "/" +
|
||||
// now.getHour() + "/" +
|
||||
// now.getMinute() + "/";
|
||||
// return url;
|
||||
// }
|
||||
//
|
||||
// public static String getOssFilePath(String filePath) {
|
||||
// String fileSuf = filePath.substring(filePath.indexOf(".") + 1);
|
||||
// return getOssDefaultPath() + UUID.randomUUID().toString() + "." + fileSuf;
|
||||
// }
|
||||
//
|
||||
//}
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
|
@ -44,6 +44,31 @@
|
|||
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||
</dependency>
|
||||
|
||||
|
||||
<!-- //创建测试工程,引入依赖-->
|
||||
<dependency>
|
||||
<groupId>com.aliyun.oss</groupId>
|
||||
<artifactId>aliyun-sdk-oss</artifactId>
|
||||
<version>3.17.4</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>javax.xml.bind</groupId>
|
||||
<artifactId>jaxb-api</artifactId>
|
||||
<version>2.3.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.activation</groupId>
|
||||
<artifactId>activation</artifactId>
|
||||
<version>1.1.1</version>
|
||||
</dependency>
|
||||
<!-- no more than 2.3.3-->
|
||||
<dependency>
|
||||
<groupId>org.glassfish.jaxb</groupId>
|
||||
<artifactId>jaxb-runtime</artifactId>
|
||||
<version>2.3.3</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Mysql Connector -->
|
||||
<dependency>
|
||||
<groupId>com.mysql</groupId>
|
||||
|
@ -90,6 +115,7 @@
|
|||
</dependency>
|
||||
|
||||
|
||||
|
||||
<!-- <!– XllJob定时任务 –>-->
|
||||
<!-- <dependency>-->
|
||||
<!-- <groupId>com.muyu</groupId>-->
|
||||
|
|
|
@ -210,8 +210,7 @@ public class EngIneController extends BaseController {
|
|||
*/
|
||||
@PostMapping("/generate")
|
||||
public EngineVersion generate(@RequestBody EngineVersion engineVersion) {
|
||||
EngineVersion byId1 = engineVersionService.getById(engineVersion.getId());
|
||||
EngineMaintenance byId = engIneService.selectById(byId1.getId());
|
||||
EngineMaintenance byId = engIneService.selectById(engineVersion.getId());
|
||||
byId.setName("generate" + "_" + byId.getEngineCode() + "_" + engineVersion.getVersionCode());
|
||||
engineVersion.setRuleContent(GenerateConstant.generateConstant(byId, engineVersion));
|
||||
return engineVersion;
|
||||
|
|
|
@ -0,0 +1,55 @@
|
|||
package com.muyu.input;
|
||||
|
||||
import com.aliyun.oss.ClientException;
|
||||
import com.aliyun.oss.OSS;
|
||||
import com.aliyun.oss.common.auth.*;
|
||||
import com.aliyun.oss.OSSClientBuilder;
|
||||
import com.aliyun.oss.OSSException;
|
||||
import com.aliyun.oss.model.PutObjectRequest;
|
||||
import com.aliyun.oss.model.PutObjectResult;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.InputStream;
|
||||
|
||||
public class Demo {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
// Endpoint以华东1(杭州)为例,其它Region请按实际情况填写。
|
||||
String endpoint = "https://oss-cn-shanghai.aliyuncs.com";
|
||||
// 从环境变量中获取访问凭证。运行本代码示例之前,请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
|
||||
EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
|
||||
// 填写Bucket名称,例如examplebucket。
|
||||
String bucketName = "011811";
|
||||
// 填写Object完整路径,完整路径中不能包含Bucket名称,例如exampledir/exampleobject.txt。
|
||||
String objectName = "cloud-etl-engine\\cloud-etl-engine\\cloud-etl-server\\target\\cloud-engine.jar";
|
||||
// 填写本地文件的完整路径,例如D:\\localpath\\examplefile.txt。
|
||||
// 如果未指定本地路径,则默认从示例程序所属项目对应本地路径中上传文件流。
|
||||
String filePath= "D:\\cloud-etl-engine\\cloud-etl-engine\\cloud-etl-server\\target\\cloud-engine.jar";
|
||||
|
||||
// 创建OSSClient实例。
|
||||
OSS ossClient = new OSSClientBuilder().build(endpoint, credentialsProvider);
|
||||
|
||||
try {
|
||||
InputStream inputStream = new FileInputStream(filePath);
|
||||
// 创建PutObjectRequest对象。
|
||||
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectName, inputStream);
|
||||
// 创建PutObject请求。
|
||||
PutObjectResult result = ossClient.putObject(putObjectRequest);
|
||||
} catch (OSSException oe) {
|
||||
System.out.println("Caught an OSSException, which means your request made it to OSS, "
|
||||
+ "but was rejected with an error response for some reason.");
|
||||
System.out.println("Error Message:" + oe.getErrorMessage());
|
||||
System.out.println("Error Code:" + oe.getErrorCode());
|
||||
System.out.println("Request ID:" + oe.getRequestId());
|
||||
System.out.println("Host ID:" + oe.getHostId());
|
||||
} catch (ClientException ce) {
|
||||
System.out.println("Caught an ClientException, which means the client encountered "
|
||||
+ "a serious internal problem while trying to communicate with OSS, "
|
||||
+ "such as not being able to access the network.");
|
||||
System.out.println("Error Message:" + ce.getMessage());
|
||||
} finally {
|
||||
if (ossClient != null) {
|
||||
ossClient.shutdown();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
4
pom.xml
4
pom.xml
|
@ -22,8 +22,8 @@
|
|||
<module>cloud-etl-server</module>
|
||||
</modules>
|
||||
<properties>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
<maven.compiler.target>17</maven.compiler.target>
|
||||
<maven.compiler.parameters>true</maven.compiler.parameters>
|
||||
</properties>
|
||||
</project>
|
||||
|
|
Loading…
Reference in New Issue