Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
glsx-web
/
glsk-oss-jssdk
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 f9ac2c03
authored
Jul 11, 2025
by
李伟康
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
提交
1 parent
6a7598e1
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
29 additions
and
15 deletions
dist/glsk-oss-jssdk.js
package.json
src/index.js
dist/glsk-oss-jssdk.js
View file @
f9ac2c0
!
function
(
e
,
s
){
"object"
==
typeof
exports
&&
"undefined"
!=
typeof
module
?
module
.
exports
=
s
():
"function"
==
typeof
define
&&
define
.
amd
?
define
(
s
):(
e
=
e
||
self
)[
"glsk-oss-jssdk"
]
=
s
()}(
this
,
function
(){
"use strict"
;
const
o
=
require
(
"ali-oss"
);
return
class
{
constructor
(
e
=
{}){
this
.
baseConfig
=
e
.
baseConfig
}
multipartUpload
(
t
,
n
=
new
Function
,
c
=
new
Function
,
e
,
i
=
"image"
,
r
=
500
,
a
=
3
){
var
s
=
this
[
"baseConfig"
];
s
||
c
(
new
Error
(
"初始化缺少baseConfig配置"
));
const
l
=
t
.
name
,
d
=
new
o
({
accessKeyId
:
s
.
alyOss
.
accessKeyId
,
accessKeySecret
:
s
.
alyOss
.
accessKeySecret
,
bucket
:
s
.
alyOss
.
bucket
,
endpoint
:
s
.
alyOss
.
endpoint
});
if
(
r
<
100
)
console
.
error
(
"分片大小不能小于100KB"
),
c
(
new
Error
(
"分片大小不能小于100KB"
));
else
{
i
.
endsWith
(
"/"
)
||
(
i
+=
"/"
);
const
u
=
i
+
l
;
let
s
=
0
,
o
=
null
;
const
p
=
()
=>
{
d
.
multipartUpload
(
u
,
t
,{
partSize
:
1024
*
r
,
checkpoint
:
o
,
progress
:
function
(
e
,
s
){
o
=
s
,
console
.
log
(
`上传进度:
${
100
*
e
}
%`
)}}).
then
(
e
=>
{
var
s
=
1
e6
<
t
.
size
?
parseFloat
(
t
.
size
/
1
e6
).
toFixed
(
2
)
+
"M"
:
parseFloat
(
t
.
size
/
1
e3
).
toFixed
(
2
)
+
"KB"
,[
e
]
=
(
console
.
log
(
e
.
res
),
e
.
res
.
requestUrls
);
n
({
attachment
:
l
,
aliyunAddress
:
decodeURIComponent
(
e
).
split
(
"?"
)[
0
],
size
:
s
})}).
catch
(
e
=>
{
console
.
error
(
"分片上传失败"
,
e
),
s
<
a
?(
s
++
,
console
.
log
(
`第
${
s
}
次重试上传...`
),
setTimeout
(()
=>
{
p
()},
3
e3
)):
c
(
e
)})};
p
()}
}}});
!
function
(
e
,
s
){
"object"
==
typeof
exports
&&
"undefined"
!=
typeof
module
?
module
.
exports
=
s
():
"function"
==
typeof
define
&&
define
.
amd
?
define
(
s
):(
e
=
e
||
self
)[
"glsk-oss-jssdk"
]
=
s
()}(
this
,
function
(){
"use strict"
;
const
y
=
require
(
"ali-oss"
);
return
class
{
constructor
(
e
=
{}){
this
.
baseConfig
=
e
.
baseConfig
}
multipartUpload
(
o
,
t
=
new
Function
,
s
=
new
Function
,
e
,
n
=
"image"
,
r
=
500
,
c
=
3
){
var
i
=
this
[
"baseConfig"
];
i
||
s
(
new
Error
(
"初始化缺少baseConfig配置"
));
const
l
=
o
.
name
,
a
=
new
y
({
accessKeyId
:
i
.
alyOss2
.
accessKeyId
,
accessKeySecret
:
i
.
alyOss2
.
accessKeySecret
,
bucket
:
i
.
alyOss2
.
bucket
,
endpoint
:
i
.
alyOss2
.
endpoint
});
if
(
r
<
100
)
return
console
.
error
(
"分片大小不能小于100KB"
),
void
s
(
new
Error
(
"分片大小不能小于100KB"
));
n
.
endsWith
(
"/"
)
||
(
n
+=
"/"
);
const
p
=
n
+
l
;
let
d
=
0
,
u
=
null
;
const
f
=
()
=>
{
a
.
multipartUpload
(
p
,
o
,{
partSize
:
1024
*
r
,
checkpoint
:
u
,
progress
:
function
(
e
,
s
){
u
=
s
,
console
.
log
(
`上传进度:
${
100
*
e
}
%`
)}}).
then
(
e
=>
{
var
s
=
1
e6
<
o
.
size
?
parseFloat
(
o
.
size
/
1
e6
).
toFixed
(
2
)
+
"M"
:
parseFloat
(
o
.
size
/
1
e3
).
toFixed
(
2
)
+
"KB"
,[
e
]
=
(
console
.
log
(
e
.
res
),
e
.
res
.
requestUrls
),
e
=
(
console
.
log
(
e
,
"aliyunAddress"
),
e
.
split
(
"?"
)[
0
].
split
(
"https:"
)[
1
]);
console
.
log
(
e
,
"url"
),
t
({
attachment
:
l
,
aliyunAddress
:
"https:"
+
g
(
decodeURIComponent
(
e
)),
size
:
s
})}).
catch
(
e
=>
{
console
.
error
(
"分片上传失败"
,
e
),
d
<
c
?(
d
++
,
console
.
log
(
`第
${
d
}
次重试上传...`
),
setTimeout
(()
=>
{
f
()},
3
e3
)):
s
(
e
)})},
g
=
e
=>
{
var
s
=
new
RegExp
(
`[
${
'? \\ * | " < > [ ] { } % # & + = ^ ` ~ ; '
.
replace
(
/ /g
,
""
).
replace
(
/
(
.
)
/g
,
"\\$1"
)}
]`
,
"g"
);
return
e
.
replace
(
s
,
e
=>
{
e
=
e
.
charCodeAt
(
0
);
return
32
===
e
?
"%20"
:
"%"
+
e
.
toString
(
16
).
toUpperCase
()})};
f
()
}}});
package.json
View file @
f9ac2c0
{
"name"
:
"glsk-oss-jssdk"
,
"version"
:
"1.0.
2
"
,
"version"
:
"1.0.
3
"
,
"description"
:
"glsk-oss-jssdk"
,
"main"
:
"src/index.js"
,
"scripts"
:
{
...
...
src/index.js
View file @
f9ac2c0
...
...
@@ -23,31 +23,26 @@ class GlOssApi {
maxRetries
=
3
// 最大重试次数
)
{
const
{
baseConfig
}
=
this
if
(
!
baseConfig
)
errCallBack
(
new
Error
(
'初始化缺少baseConfig配置'
))
if
(
!
baseConfig
)
errCallBack
(
new
Error
(
"初始化缺少baseConfig配置"
))
const
fileName
=
file
.
name
const
client
=
new
OSS
({
accessKeyId
:
baseConfig
.
alyOss
.
accessKeyId
,
accessKeySecret
:
baseConfig
.
alyOss
.
accessKeySecret
,
bucket
:
baseConfig
.
alyOss
.
bucket
,
endpoint
:
baseConfig
.
alyOss
.
endpoint
accessKeyId
:
baseConfig
.
alyOss
2
.
accessKeyId
,
accessKeySecret
:
baseConfig
.
alyOss
2
.
accessKeySecret
,
bucket
:
baseConfig
.
alyOss
2
.
bucket
,
endpoint
:
baseConfig
.
alyOss
2
.
endpoint
})
if
(
partSize
<
100
)
{
console
.
error
(
"分片大小不能小于100KB"
)
errCallBack
(
new
Error
(
"分片大小不能小于100KB"
))
return
}
// 确保目录以斜杠结尾
if
(
!
dir
.
endsWith
(
"/"
))
{
dir
+=
"/"
}
const
key
=
dir
+
fileName
let
retryCount
=
0
let
currentCheckpoint
=
null
const
uploadWithRetry
=
()
=>
{
client
.
multipartUpload
(
key
,
file
,
{
...
...
@@ -63,18 +58,20 @@ class GlOssApi {
file
.
size
>
1000000
?
parseFloat
(
file
.
size
/
1000000
).
toFixed
(
2
)
+
"M"
:
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
({
attachment
:
fileName
,
aliyunAddress
:
decodeURIComponent
(
aliyunAddress
).
split
(
'?'
)[
0
],
aliyunAddress
:
"https:"
+
encodeOSSFilename
(
decodeURIComponent
(
url
)),
size
:
size
})
})
.
catch
((
err
)
=>
{
console
.
error
(
"分片上传失败"
,
err
)
if
(
retryCount
<
maxRetries
)
{
retryCount
++
console
.
log
(
`第
${
retryCount
}
次重试上传...`
)
...
...
@@ -86,7 +83,24 @@ class GlOssApi {
}
})
}
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
()}
`
})
}
uploadWithRetry
()
// 启动上传流程
}
}
...
...
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