Oss上传测试类
parent
5503185da1
commit
80b9f79da4
|
@ -26,6 +26,12 @@
|
||||||
<groupId>com.muyu</groupId>
|
<groupId>com.muyu</groupId>
|
||||||
<artifactId>cloud-common-security</artifactId>
|
<artifactId>cloud-common-security</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.aliyun.oss</groupId>
|
||||||
|
<artifactId>aliyun-sdk-oss</artifactId>
|
||||||
|
<version>3.17.4</version>
|
||||||
|
<scope>compile</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<!-- <dependency>-->
|
<!-- <dependency>-->
|
||||||
<!-- <groupId>org.springframework.boot</groupId>-->
|
<!-- <groupId>org.springframework.boot</groupId>-->
|
||||||
|
|
|
@ -0,0 +1,58 @@
|
||||||
|
package com.muyu.utils;
|
||||||
|
|
||||||
|
import com.aliyun.oss.OSS;
|
||||||
|
|
||||||
|
import com.aliyun.oss.OSSClientBuilder;
|
||||||
|
|
||||||
|
import com.aliyun.oss.model.PutObjectRequest;
|
||||||
|
|
||||||
|
import com.muyu.common.core.domain.Result;
|
||||||
|
|
||||||
|
import java.io.ByteArrayInputStream;
|
||||||
|
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
|
||||||
|
public class OssUpload {
|
||||||
|
|
||||||
|
private static final String endpoint = "https://oss-cn-shanghai.aliyuncs.com";
|
||||||
|
|
||||||
|
private static final String accessKeyId = "LTAI5t8LmMHfW8ckPaAZR6oQ";
|
||||||
|
|
||||||
|
private static final String accessKeySecret = "pwzEwkpxcTFgurkARyr7sG7V6syc9x";
|
||||||
|
|
||||||
|
private static final String bucketName = "011811";
|
||||||
|
|
||||||
|
public static Result<Object> uploadFiles(String content, String fileName) {
|
||||||
|
|
||||||
|
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
|
||||||
|
|
||||||
|
String objectName = "build/rule/version/" + fileName + ".java";
|
||||||
|
|
||||||
|
try {
|
||||||
|
ByteArrayInputStream inputStream = new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8));
|
||||||
|
|
||||||
|
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectName, inputStream);
|
||||||
|
|
||||||
|
ossClient.putObject(putObjectRequest);
|
||||||
|
|
||||||
|
return Result.success(null, "文件" + fileName + ".java" + "存储成功");
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
|
||||||
|
e.printStackTrace();
|
||||||
|
|
||||||
|
return Result.error(null, "文件" + fileName + ".java" + "存储失败");
|
||||||
|
|
||||||
|
} finally {
|
||||||
|
|
||||||
|
if (ossClient != null) {
|
||||||
|
|
||||||
|
ossClient.shutdown();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -14,6 +14,7 @@ import java.io.InputStream;
|
||||||
public class Demo {
|
public class Demo {
|
||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
|
|
||||||
// Endpoint以华东1(杭州)为例,其它Region请按实际情况填写。
|
// Endpoint以华东1(杭州)为例,其它Region请按实际情况填写。
|
||||||
String endpoint = "https://oss-cn-shanghai.aliyuncs.com";
|
String endpoint = "https://oss-cn-shanghai.aliyuncs.com";
|
||||||
// 从环境变量中获取访问凭证。运行本代码示例之前,请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
|
// 从环境变量中获取访问凭证。运行本代码示例之前,请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
|
||||||
|
|
Loading…
Reference in New Issue