Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
jinli gu
Jeepay
Commits
f19f27cd
Commit
f19f27cd
authored
Apr 25, 2022
by
陈泉
Browse files
修复OSS保存路径未考虑设置前缀
parent
c83e7895
Changes
1
Hide whitespace changes
Inline
Side-by-side
jeepay-components/jeepay-components-oss/src/main/java/com/jeequan/jeepay/components/oss/service/AliyunOssService.java
View file @
f19f27cd
...
...
@@ -19,6 +19,7 @@ import com.aliyun.oss.OSS;
import
com.aliyun.oss.OSSClientBuilder
;
import
com.aliyun.oss.model.GetObjectRequest
;
import
com.jeequan.jeepay.components.oss.config.AliyunOssYmlConfig
;
import
com.jeequan.jeepay.components.oss.config.OssYmlConfig
;
import
com.jeequan.jeepay.components.oss.constant.OssSavePlaceEnum
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.beans.factory.annotation.Autowired
;
...
...
@@ -30,44 +31,83 @@ import javax.annotation.PostConstruct;
import
java.io.File
;
/**
* 阿里云OSS 实现类
*
* @author terrfly
* @site https://www.jeequan.com
* @date 2021/7/12 18:20
*/
* 阿里云OSS 实现类
*
* @author terrfly
* @site https://www.jeequan.com
* @date 2021/7/12 18:20
*/
@Service
@Slf4j
@ConditionalOnProperty
(
name
=
"isys.oss.service-type"
,
havingValue
=
"aliyun-oss"
)
public
class
AliyunOssService
implements
IOssService
{
public
class
AliyunOssService
implements
IOssService
{
@Autowired
private
AliyunOssYmlConfig
aliyunOssYmlConfig
;
@Autowired
private
AliyunOssYmlConfig
aliyunOssYmlConfig
;
@Autowired
private
OssYmlConfig
ossYmlConfig
;
// ossClient 初始化
private
OSS
ossClient
=
null
;
@PostConstruct
public
void
init
(){
ossClient
=
new
OSSClientBuilder
().
build
(
aliyunOssYmlConfig
.
getEndpoint
(),
aliyunOssYmlConfig
.
getAccessKeyId
(),
aliyunOssYmlConfig
.
getAccessKeySecret
());
public
void
init
()
{
ossClient
=
new
OSSClientBuilder
().
build
(
aliyunOssYmlConfig
.
getEndpoint
(),
aliyunOssYmlConfig
.
getAccessKeyId
(),
aliyunOssYmlConfig
.
getAccessKeySecret
());
}
/**
* 处理文件保存路径
*
* @param ossSavePlaceEnum 保存位置
* @param filePath 文件路径
* @return 完整路径
*/
private
String
getFileKey
(
OssSavePlaceEnum
ossSavePlaceEnum
,
String
filePath
)
{
// 上传的时候需要考虑 OSS 存储空间的访问权限,并拼接路径前缀
String
filePrefix
=
ossSavePlaceEnum
==
OssSavePlaceEnum
.
PUBLIC
?
ossYmlConfig
.
getOss
()
.
getFilePublicPath
()
:
ossYmlConfig
.
getOss
().
getFilePrivatePath
();
// 如果路径包含设置的路径前缀,则跳过
if
(
filePath
.
startsWith
(
filePrefix
))
{
// OSS 不允许路径第一个字符为 /
if
(
filePath
.
indexOf
(
"/"
)
==
0
)
{
filePath
=
filePath
.
replaceFirst
(
"/"
,
""
);
}
return
filePath
;
}
String
fullPath
=
(
filePrefix
+
"/"
+
filePath
);
// OSS 不允许路径第一个字符为 /
if
(
fullPath
.
indexOf
(
"/"
)
==
0
)
{
fullPath
=
fullPath
.
replaceFirst
(
"/"
,
""
);
}
return
fullPath
;
}
@Override
public
String
upload2PreviewUrl
(
OssSavePlaceEnum
ossSavePlaceEnum
,
MultipartFile
multipartFile
,
String
saveDirAndFileName
)
{
public
String
upload2PreviewUrl
(
OssSavePlaceEnum
ossSavePlaceEnum
,
MultipartFile
multipartFile
,
String
saveDirAndFileName
)
{
try
{
this
.
ossClient
.
putObject
(
ossSavePlaceEnum
==
OssSavePlaceEnum
.
PUBLIC
?
aliyunOssYmlConfig
.
getPublicBucketName
()
:
aliyunOssYmlConfig
.
getPrivateBucketName
()
,
saveDirAndFileName
,
multipartFile
.
getInputStream
());
String
fullPath
=
getFileKey
(
ossSavePlaceEnum
,
saveDirAndFileName
);
if
(
ossSavePlaceEnum
==
OssSavePlaceEnum
.
PUBLIC
){
this
.
ossClient
.
putObject
(
ossSavePlaceEnum
==
OssSavePlaceEnum
.
PUBLIC
?
aliyunOssYmlConfig
.
getPublicBucketName
()
:
aliyunOssYmlConfig
.
getPrivateBucketName
()
,
fullPath
,
multipartFile
.
getInputStream
());
if
(
ossSavePlaceEnum
==
OssSavePlaceEnum
.
PUBLIC
)
{
// 文档:https://www.alibabacloud.com/help/zh/doc-detail/39607.htm example: https://BucketName.Endpoint/ObjectName
return
"https://"
+
aliyunOssYmlConfig
.
getPublicBucketName
()
+
"."
+
aliyunOssYmlConfig
.
getEndpoint
()
+
"/"
+
saveDirAndFileName
;
return
"https://"
+
aliyunOssYmlConfig
.
getPublicBucketName
()
+
"."
+
aliyunOssYmlConfig
.
getEndpoint
()
+
"/"
+
fullPath
;
}
return
saveDirAndFileName
;
}
catch
(
Exception
e
)
{
log
.
error
(
"error"
,
e
);
log
.
error
(
"error"
,
e
);
return
null
;
}
}
...
...
@@ -76,9 +116,21 @@ public class AliyunOssService implements IOssService{
public
boolean
downloadFile
(
OssSavePlaceEnum
ossSavePlaceEnum
,
String
source
,
String
target
)
{
try
{
String
fullPath
=
getFileKey
(
ossSavePlaceEnum
,
source
);
File
downloadFile
=
new
File
(
target
);
// 当本地路径的上层目录不存在时,自动创建
// OSS SDK 在 Docker 内部可能出现 UnknownHost 错误具体表现为
// com.aliyun.oss.ClientException: Cannot read the content input stream.
if
(!
downloadFile
.
getParentFile
().
exists
())
{
log
.
info
(
"downloadFile parent dir not exists create it: {}"
,
downloadFile
.
getParentFile
().
getAbsolutePath
());
downloadFile
.
getParentFile
().
mkdirs
();
}
String
bucket
=
ossSavePlaceEnum
==
OssSavePlaceEnum
.
PRIVATE
?
aliyunOssYmlConfig
.
getPrivateBucketName
()
:
aliyunOssYmlConfig
.
getPublicBucketName
();
this
.
ossClient
.
getObject
(
new
GetObjectRequest
(
bucket
,
source
),
new
File
(
target
)
);
this
.
ossClient
.
getObject
(
new
GetObjectRequest
(
bucket
,
fullPath
),
downloadFile
);
return
true
;
}
catch
(
Exception
e
)
{
...
...
Write
Preview
Supports
Markdown
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