Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
M
mall-server
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
JIRA
JIRA
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
yimu
mall-server
Commits
053cac34
Commit
053cac34
authored
Dec 31, 2021
by
秦兴亮
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
添加obs
parent
0b863b26
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
155 additions
and
1 deletion
+155
-1
pom.xml
mall-server-front/pom.xml
+6
-0
UploadController.java
...mall/server/front/system/controller/UploadController.java
+2
-1
OBSUtil.java
...src/main/java/com/fzm/mall/server/front/util/OBSUtil.java
+147
-0
No files found.
mall-server-front/pom.xml
View file @
053cac34
...
...
@@ -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>
...
...
mall-server-front/src/main/java/com/fzm/mall/server/front/system/controller/UploadController.java
View file @
053cac34
...
...
@@ -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
O
S
SUtil
.
uploadByte
(
bytes
,
""
,
SerialIdsUtil
.
createFileName
(
fileType
));
return
O
B
SUtil
.
uploadByte
(
bytes
,
""
,
SerialIdsUtil
.
createFileName
(
fileType
));
}
private
String
getFileType
(
String
fileName
)
{
...
...
mall-server-front/src/main/java/com/fzm/mall/server/front/util/OBSUtil.java
0 → 100644
View file @
053cac34
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
;
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment