Commit 053cac34 authored by 秦兴亮's avatar 秦兴亮

添加obs

parent 0b863b26
......@@ -103,6 +103,12 @@
<version>1.68</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.huaweicloud</groupId>
<artifactId>esdk-obs-java</artifactId>
<version>3.19.7</version>
</dependency>
</dependencies>
......
......@@ -2,6 +2,7 @@ package com.fzm.mall.server.front.system.controller;
import com.fzm.mall.server.front.base.ResponseVO;
import com.fzm.mall.server.front.constant.MallResponseEnum;
import com.fzm.mall.server.front.util.OBSUtil;
import com.fzm.mall.server.front.util.OSSUtil;
import com.fzm.mall.server.front.util.SerialIdsUtil;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
......@@ -69,7 +70,7 @@ public class UploadController {
log.error("upload error:", e);
e.printStackTrace();
}
return OSSUtil.uploadByte(bytes, "", SerialIdsUtil.createFileName(fileType));
return OBSUtil.uploadByte(bytes, "", SerialIdsUtil.createFileName(fileType));
}
private String getFileType(String fileName) {
......
package com.fzm.mall.server.front.util;
import com.obs.services.ObsClient;
import com.obs.services.model.ObjectMetadata;
import java.io.*;
import java.net.URL;
/**
* @author wangp
* @date 2021/5/25 17:47
* @description 华为云对象存储
* @since JDK 1.8
*/
public class OBSUtil {
private static String accessKey = "HIULSHJZJOTWB72Z38ZB"; //取值为获取的AK LTAI4GCoj6DGZx5JgUYVLs3P
private static String securityKey = "GI0oWEGRJuMKmPXJq2fCmuMpFPIITLmjeCQxo2tA"; //取值为获取的SK 7TEBqdFmFtKT7AFDhh1DNgdoUSosYJ
private static String endpoint = "https://obs.cn-east-3.myhuaweicloud.com"; //终端节点(Endpoint)
private static String region = "cn-east-3"; // 取值为规划桶所在的区域
private static String bucket = "yimu-file"; //存储桶
private static String baseUrl = "https://yimu-file.obs.cn-east-3.myhuaweicloud.com/";
public static void main(String[] str) {
String S = uploadFile("C:\\Users\\bl\\Desktop\\链命令.txt", "manage/img/ttt", "a.txt");
System.err.println(S); }
// 创建ObsClient实例
public static ObsClient getObsClient() {
return new ObsClient(accessKey, securityKey, endpoint);
}
/**
* 上传Byte数组
*
* @param content 待上传字节数组
* @param folder OSS文件夹名
* @param fileName OSS文件名
* @return OSS访问地址
*/
public static String uploadByte(byte[] content, String folder, String fileName) {
// 创建ObsClient实例。
ObsClient obsClient = getObsClient();
// 上传Byte数组。
obsClient.putObject(bucket, folder + fileName, new ByteArrayInputStream(content));
// 关闭obsClient
try {
obsClient.close();
} catch (IOException e) {
e.printStackTrace();
}
return baseUrl + folder + fileName;
}
/**
* 上传网络流
*
* @param spec 网络地址
* @param folder OSS文件夹名
* @param fileName OSS文件名
* @return OSS访问地址
*/
public static String uploadNet(String spec, String folder, String fileName) {
// 创建ObsClient实例。
ObsClient obsClient = getObsClient();
// 上传网络流。
InputStream inputStream = null;
try {
inputStream = new URL(spec).openStream();
} catch (IOException e) {
e.printStackTrace();
}
obsClient.putObject(bucket, folder + fileName, inputStream);
// 关闭obsClient
try {
obsClient.close();
} catch (IOException e) {
e.printStackTrace();
}
return baseUrl + folder + fileName;
}
/**
* 上传文件流
*
* @param filePath 待上传文件路径
* @param folder OSS文件夹名
* @param fileName OSS文件名
* @return OSS访问地址
*/
public static String uploadFile(String filePath, String folder, String fileName) {
// 创建ObsClient实例。
ObsClient obsClient = getObsClient();
System.err.println("obsClient:" + obsClient);
// 上传文件流。
FileInputStream fis = null; // 待上传的本地文件路径,需要指定到具体的文件名
try {
fis = new FileInputStream(new File(filePath));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
obsClient.putObject(bucket, folder + fileName, fis);
// 关闭obsClient
try {
obsClient.close();
} catch (IOException e) {
e.printStackTrace();
}
return baseUrl + folder + fileName;
}
/**
* 上传文件流
*
* @param in 待上传文件流
* @param folder OSS文件夹名
* @param fileName OSS文件名
* @return OSS访问地址
*/
public static String uploadStream(InputStream in, String folder, String fileName) {
// 创建ObsClient实例。
ObsClient obsClient = getObsClient();
// 上传Byte数组。
obsClient.putObject(bucket, folder + fileName, in);
// 关闭obsClient
try {
obsClient.close();
} catch (IOException e) {
e.printStackTrace();
}
return baseUrl + folder + fileName;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment