Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
saas_plus
/
dj-car-life-system
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit 34aeab6c
authored
Aug 22, 2025
by
前端
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
还原财资数据OSS上传被覆盖的代码
1 parent
fd5ce091
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
32 additions
and
80 deletions
src/common/oss.js
src/common/oss.js
View file @
34aeab6
import
{
baseConfig
}
from
"@/main"
const
OSS
=
require
(
"ali-oss"
)
const
OSS
=
require
(
"ali-oss"
)
class
GlOssApi
{
export
default
{
constructor
(
params
=
{})
{
this
.
baseConfig
=
params
.
baseConfig
}
/**
/**
*
分片上传大文件到阿里云OSS(带断点续传和重试机制)
*
阿里云oss sdk文件上传 dir 上传到的文件位置
* @param {*} file 文件流
(大文件)
* @param {*} file 文件流
* @param {*} successCallback 成功回调
* @param {*} successCallback 成功回调
* @param {*} errCallBack 失败回调
* @param {*} errCallBack 失败回调
* @param {*} bucketName 桶名(可选)
* @param {*} bucketName 阿里云桶名(可以指定多个桶名)
* @param {*} dir 上传目录(可选)
* @param {*} dir 上传文件夹路径 譬如images
* @param {*} partSize 分片大小(单位 KB,默认500KB)
* @param {*} maxRetries 最大重试次数(默认3次)
*/
*/
multipartUpload
(
bucketUpload
(
file
,
file
,
successCallback
=
new
Function
(),
successCallback
=
new
Function
(),
errCallBack
=
new
Function
(),
errCallBack
=
new
Function
(),
bucketName
=
"zzl-filerepostorypublic"
,
bucketName
=
"zzl-filerepostorypublic"
,
dir
=
"image"
,
dir
=
"image"
partSize
=
500
,
// 默认分片大小为500KB
maxRetries
=
3
// 最大重试次数
)
{
)
{
const
{
baseConfig
}
=
this
const
fileName
=
file
.
name
// 先获取上传要的资料签名
if
(
!
baseConfig
)
errCallBack
(
new
Error
(
"初始化缺少baseConfig配置"
))
// 实例化一个上传客户端
const
fileName
=
file
.
name
const
client
=
new
OSS
({
const
client
=
new
OSS
({
accessKeyId
:
baseConfig
.
alyOss2
.
accessKeyId
,
// yourRegion填写Bucket所在地域。Region填写为oss-cn-hangzhou。
accessKeySecret
:
baseConfig
.
alyOss2
.
accessKeySecret
,
// region: "oss-cn-beijing", //我的存储是北京,可根据具体配置自行修改
bucket
:
baseConfig
.
alyOss2
.
bucket
,
// region: "oss-cn-shanghai",
endpoint
:
baseConfig
.
alyOss2
.
endpoint
// 从STS服务获取的临时访问密钥(AccessKey ID和AccessKey Secret)。)(后台接口返回)
accessKeyId
:
baseConfig
.
alyOss
.
accessKeyId
,
accessKeySecret
:
baseConfig
.
alyOss
.
accessKeySecret
,
// 从STS服务获取的安全令牌(SecurityToken)。
// stsToken: obj.securityToken,
// 填写Bucket名称。
bucket
:
baseConfig
.
alyOss
.
bucket
,
endpoint
:
baseConfig
.
alyOss
.
endpoint
})
})
if
(
partSize
<
100
)
{
console
.
error
(
"分片大小不能小于100KB"
)
errCallBack
(
new
Error
(
"分片大小不能小于100KB"
))
return
}
// 确保目录以斜杠结尾
try
{
if
(
!
dir
.
endsWith
(
"/"
))
{
// 为保证唯一性,可以通过uuid将文件名替换
// let uuid = uuidv4() + fileName.substring(fileName.lastIndexOf('.'))if (dir.substring(dir.length - 1, 1) !== '/') {
dir
+=
"/"
dir
+=
"/"
}
const
result
=
client
.
put
(
dir
+
fileName
,
file
)
const
uniqueDir
=
result
dir
+
Date
.
now
()
+
"_"
+
Math
.
floor
(
Math
.
random
()
*
10000
)
+
"/"
.
then
((
res
)
=>
{
const
key
=
uniqueDir
+
fileName
console
.
log
(
res
,
"返回文件名和文件地址"
)
let
retryCount
=
0
let
currentCheckpoint
=
null
const
uploadWithRetry
=
()
=>
{
client
.
multipartUpload
(
key
,
file
,
{
partSize
:
partSize
*
1024
,
// 转换为字节
checkpoint
:
currentCheckpoint
,
// 使用上次的断点信息
progress
:
function
(
p
,
checkpoint
)
{
currentCheckpoint
=
checkpoint
console
.
log
(
`上传进度:
${
p
*
100
}
%`
)
}
})
.
then
((
result
)
=>
{
const
size
=
const
size
=
file
.
size
>
1000000
file
.
size
>
1000000
?
parseFloat
(
file
.
size
/
1000000
).
toFixed
(
2
)
+
"M"
?
parseFloat
(
file
.
size
/
1000000
).
toFixed
(
2
)
+
"M"
:
parseFloat
(
file
.
size
/
1000
).
toFixed
(
2
)
+
"KB"
:
parseFloat
(
file
.
size
/
1000
).
toFixed
(
2
)
+
"KB"
console
.
log
(
result
.
res
)
let
[
aliyunAddress
]
=
result
.
res
.
requestUrls
console
.
log
(
aliyunAddress
,
"aliyunAddress"
)
let
url
=
aliyunAddress
.
split
(
"?"
)[
0
].
split
(
"https:"
)[
1
]
console
.
log
(
url
,
"url"
)
successCallback
({
successCallback
({
attachment
:
fileName
,
attachment
:
fileName
,
aliyunAddress
:
aliyunAddress
:
res
.
url
,
"https:"
+
encodeOSSFilename
(
decodeURIComponent
(
url
)),
size
:
size
size
:
size
})
})
})
})
.
catch
((
err
)
=>
{
.
catch
((
err
)
=>
{
console
.
error
(
"分片上传失败"
,
err
)
if
(
retryCount
<
maxRetries
)
{
retryCount
++
console
.
log
(
`第
${
retryCount
}
次重试上传...`
)
setTimeout
(()
=>
{
uploadWithRetry
()
},
3000
)
// 3秒后重试
}
else
{
errCallBack
(
err
)
errCallBack
(
err
)
}
})
}
const
encodeOSSFilename
=
(
filename
)
=>
{
// 定义需要转码的特殊字符(包括空格)
const
specialChars
=
'? \\ * | " < > [ ] { } % # & + = ^ ` ~ ; '
// 创建正则表达式匹配所有特殊字符
const
escapeRegex
=
new
RegExp
(
`[
${
specialChars
.
replace
(
/ /g
,
""
).
replace
(
/
(
.
)
/g
,
"\\$1"
)}
]`
,
"g"
)
// 转码函数:将字符转为百分号编码
return
filename
.
replace
(
escapeRegex
,
(
char
)
=>
{
// 获取字符的UTF-8编码
const
code
=
char
.
charCodeAt
(
0
)
// 空格特殊处理为%20,其他转为大写十六进制
return
code
===
32
?
"%20"
:
`%
${
code
.
toString
(
16
).
toUpperCase
()}
`
})
})
}
catch
(
e
)
{
console
.
log
(
e
)
}
}
uploadWithRetry
()
// 启动上传流程
}
}
}
}
export
default
GlOssApi
Write
Preview
Markdown
is supported
Attach a file
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 post a comment