Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
glsx
/
gl_tape
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
3
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit 73664db4
authored
Jun 27, 2024
by
yixinjj
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
添加注释
1 parent
098d35a5
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
157 additions
and
94 deletions
hybrid/html/index.html
hybrid/html/js/OSSUpLoader.js
hybrid/html/index.html
View file @
73664db
<!DOCTYPE html>
<!DOCTYPE html>
<html>
<html>
<head>
<head>
<meta
charset=
"utf-8"
/>
<meta
charset=
"utf-8"
/>
<meta
name=
"viewport"
content=
"width=device-width, initial-scale=1"
>
<meta
name=
"viewport"
content=
"width=device-width, initial-scale=1"
>
<title>
file upload oss
</title>
<title>
file upload oss
</title>
</head>
</head>
<body>
<body>
<script
src=
"js/uni.webview.1.5.5.js"
></script>
<script
src=
"js/uni.webview.1.5.5.js"
></script>
<script
src=
"js/aliyun-oss-sdk-6.18.1.min.js"
></script>
<script
src=
"js/aliyun-oss-sdk-6.18.1.min.js"
></script>
<script
src=
"js/OSSUpLoader.js"
></script>
<script
src=
"js/OSSUpLoader.js"
></script>
<script>
<script>
// document.addEventListener('UniAppJSBridgeReady', function(){
// document.addEventListener('UniAppJSBridgeReady', function(){
window
.
msgFromUniapp
=
function
(
accessKeyId
,
accessKeySecret
,
stsToken
,
fileName
,
options
=
{})
{
/**
* 该函数用于接收来自 UniApp 的消息,它包含了文件上传所需的所有信息
* @param {string} accessKeyId - OSS 访问密钥 ID
* @param {string} accessKeySecret - OSS 访问密钥 secret
* @param {string} stsToken - 安全令牌服务(STS)令牌,用于临时访问 OSS 存储桶
* @param {string} fileName - 要上传的文件名
* @param {Object} options - (可选)包含上传器配置的对象,如 region
* @throws {Error} 如果缺少必要的参数,将抛出错误
*/
window
.
msgFromUniapp
=
function
(
accessKeyId
,
accessKeySecret
,
stsToken
,
fileName
,
options
=
{})
{
if
(
!
accessKeyId
||
!
accessKeySecret
||
!
stsToken
||
!
fileName
)
{
if
(
!
accessKeyId
||
!
accessKeySecret
||
!
stsToken
||
!
fileName
)
{
throw
new
Error
(
'accessKeyId,accessKeySecret,stsToken,fileName is required'
)
throw
new
Error
(
'accessKeyId,accessKeySecret,stsToken,fileName is required'
)
}
}
const
opt
=
Object
.
assign
({
accessKeyId
,
accessKeySecret
,
stsToken
},
options
)
try
{
let
uploader
=
new
OSSUpLoader
(
opt
)
// 使用 Object.assign 合并默认配置参数和传入的 options 参数
uploader
.
upload
(
fileName
,
function
(
result
)
{
const
opt
=
Object
.
assign
({
accessKeyId
,
accessKeySecret
,
stsToken
},
options
);
// 创建 OSSUpLoader 实例,传入配置对象
let
uploader
=
new
OSSUpLoader
(
opt
);
// 调用 upload 方法上传文件,并在完成后处理结果
uploader
.
upload
(
fileName
,
function
(
result
)
{
// 通过 uni.postMessage 将上传结果作为数据发送
uni
.
postMessage
({
uni
.
postMessage
({
data
:
{
data
:
{
...
result
,
...
result
,
fileName
fileName
}
}
});
});
uploader
.
destroy
()
// 调用 destroy 方法,这将重置上传器的状态,释放资源
uploader
=
null
uploader
.
destroy
();
})
// 将 uploader 置为 null,释放内存
uploader
=
null
;
});
}
catch
(
e
)
{
// 如果发生错误,将错误信息作为数据发送
uni
.
postMessage
({
data
:
{
code
:
1
,
fileName
,
error
:
e
.
message
}
});
}
}
// }, false);
// }, false);
</script>
</script>
</body>
</body>
</html>
</html>
\ No newline at end of file
\ No newline at end of file
hybrid/html/js/OSSUpLoader.js
View file @
73664db
...
@@ -6,15 +6,16 @@ class OSSUpLoader {
...
@@ -6,15 +6,16 @@ class OSSUpLoader {
/**
/**
* 填写Bucket名称。
* 填写Bucket名称。
*/
*/
bucket
=
'glsk-oss'
;
bucket
=
"glsk-oss"
;
/**
/**
* 文件
类型
* 文件的 MIME
类型
*/
*/
mime
=
"audio/mpeg"
;
mime
=
"audio/mpeg"
;
/**
/**
* 本地保存文件路径(待上传文件路径)
* 本地保存文件路径(待上传文件路径)
*/
*/
filePath
=
'file:///storage/emulated/0/Android/data/io.dcloud.HBuilder/apps/HBuilder/doc/'
filePath
=
"file:///storage/emulated/0/Android/data/io.dcloud.HBuilder/apps/HBuilder/doc/"
;
/**
/**
* 从STS服务获取的临时访问密钥(AccessKey ID和AccessKey Secret)。
* 从STS服务获取的临时访问密钥(AccessKey ID和AccessKey Secret)。
*/
*/
...
@@ -32,11 +33,11 @@ class OSSUpLoader {
...
@@ -32,11 +33,11 @@ class OSSUpLoader {
*/
*/
client
=
null
;
client
=
null
;
/**
/**
* 上传并发
数
* 同时上传的最大任务
数
*/
*/
parallel
=
4
;
parallel
=
4
;
/**
/**
* 设置分片大小。
500kb。
* 每个上传分片的大小(字节)。默认
500kb。
*/
*/
partSize
=
500
;
partSize
=
500
;
/**
/**
...
@@ -44,7 +45,7 @@ class OSSUpLoader {
...
@@ -44,7 +45,7 @@ class OSSUpLoader {
*/
*/
minPartSize
=
100
;
minPartSize
=
100
;
/**
/**
* 超时时间:默认60s
* 上传任务的超时时间(毫秒):默认60
*/
*/
timeout
=
60
*
1000
;
timeout
=
60
*
1000
;
// headers,
// headers,
...
@@ -57,18 +58,18 @@ class OSSUpLoader {
...
@@ -57,18 +58,18 @@ class OSSUpLoader {
*/
*/
retryCount
=
0
;
retryCount
=
0
;
/**
/**
* 失败重传
次数
* 上传失败时的最大重试
次数
*/
*/
retryCountMax
=
3
;
retryCountMax
=
3
;
/**
/**
* 失败重传间隔
* 上传失败时的重试间隔时间(毫秒)
*/
*/
retryInterval
=
3
*
1000
retryInterval
=
3
*
1000
;
currentCheckpoint
;
currentCheckpoint
;
/**
/**
* OSS文件夹
* OSS 存储桶中的目标文件夹路径
*/
*/
ossFolder
=
'quality/'
;
ossFolder
=
"quality/"
;
/**
/**
* 构造函数
* 构造函数
*/
*/
...
@@ -85,18 +86,19 @@ class OSSUpLoader {
...
@@ -85,18 +86,19 @@ class OSSUpLoader {
filePath
=
this
.
filePath
,
filePath
=
this
.
filePath
,
retryCountMax
=
this
.
retryCountMax
,
retryCountMax
=
this
.
retryCountMax
,
retryInterval
=
this
.
retryInterval
,
retryInterval
=
this
.
retryInterval
,
ossFolder
=
this
.
ossFolder
ossFolder
=
this
.
ossFolder
,
})
{
})
{
if
(
partSize
&&
partSize
<
this
.
minPartSize
)
{
if
(
partSize
<
this
.
minPartSize
)
{
throw
new
Error
(
common
.
constants
.
CODE
.
FILE_PART_SIZE_TOO_SMALL
)
throw
new
Error
(
common
.
constants
.
CODE
.
FILE_PART_SIZE_TOO_SMALL
);
}
}
this
.
mime
=
mime
this
.
mime
=
mime
;
this
.
partSize
=
partSize
this
.
partSize
=
partSize
;
this
.
parallel
=
parallel
this
.
parallel
=
parallel
;
this
.
filePath
=
filePath
this
.
timeout
=
timeout
;
this
.
retryCountMax
=
retryCountMax
this
.
filePath
=
filePath
;
this
.
retryInterval
=
retryInterval
this
.
retryCountMax
=
retryCountMax
;
this
.
ossFolder
=
ossFolder
this
.
retryInterval
=
retryInterval
;
this
.
ossFolder
=
ossFolder
;
//实例化OSS
//实例化OSS
this
.
client
=
new
OSS
({
this
.
client
=
new
OSS
({
region
,
region
,
...
@@ -106,26 +108,37 @@ class OSSUpLoader {
...
@@ -106,26 +108,37 @@ class OSSUpLoader {
stsToken
,
stsToken
,
});
});
}
}
/**文件上传函数(外部调用)
/**
* @param {Object} fileName 文件名
* 上传文件到指定的 OSS 存储空间
* @param {Object} callback 回调函数
* @param {string} fileName - 要上传的文件名
* @param {Function} callback - 文件上传完成或失败后的回调函数
* @throws {Error} 如果文件路径转换失败,将抛出错误
*/
*/
upload
(
fileName
,
callback
)
{
upload
(
fileName
,
callback
)
{
this
.
_pathToFile
(
this
.
filePath
+
fileName
,
function
(
result
)
{
this
.
_pathToFile
(
this
.
filePath
+
fileName
,
function
(
result
)
{
if
(
result
.
code
===
common
.
constants
.
CODE
.
FILE_PATH_TRANSFORM_SUCCESS
)
{
if
(
result
.
code
===
common
.
constants
.
CODE
.
FILE_PATH_TRANSFORM_SUCCESS
)
{
this
.
_upload
(
fileName
,
result
.
data
.
file
,
callback
)
this
.
_upload
(
fileName
,
result
.
data
.
file
,
callback
);
}
else
{
}
else
{
callback
(
result
)
callback
(
result
);
}
}
}.
bind
(
this
))
}.
bind
(
this
)
);
}
}
/**文件上传(内部方法)
* @param {Object} fileName 文件名
/**
* @param {Object} file 文件
* 将文件上传到对象存储服务(OSS)
* @param {Object} callback 回调函数
*
* @param {string} fileName - 要上传的文件名(包括扩展名)
* @param {File} file - 要上传的文件对象
* @param {Function} callback - 上传完成或发生错误时调用的回调函数
*
* @throws {Error} 如果文件路径转换失败,将抛出错误
*/
*/
_upload
(
fileName
,
file
,
callback
)
{
_upload
(
fileName
,
file
,
callback
)
{
this
.
client
.
multipartUpload
(
this
.
ossFolder
+
fileName
,
file
,
{
this
.
client
.
multipartUpload
(
this
.
ossFolder
+
fileName
,
file
,
{
// 获取分片上传进度、断点和返回值。
// 获取分片上传进度、断点和返回值。
progress
:
(
p
,
checkpoint
,
res
)
=>
{
progress
:
(
p
,
checkpoint
,
res
)
=>
{
this
.
currentCheckpoint
=
checkpoint
;
this
.
currentCheckpoint
=
checkpoint
;
...
@@ -137,23 +150,28 @@ class OSSUpLoader {
...
@@ -137,23 +150,28 @@ class OSSUpLoader {
partSize
:
1024
*
this
.
partSize
,
partSize
:
1024
*
this
.
partSize
,
timeout
:
this
.
timeout
,
timeout
:
this
.
timeout
,
mime
:
this
.
mime
,
mime
:
this
.
mime
,
}).
then
((
res
)
=>
{
})
.
then
((
res
)
=>
{
// console.log(JSON.stringify(res),res.status,res.status==200)
// console.log(JSON.stringify(res),res.status,res.status==200)
if
(
res
.
res
.
statusCode
==
200
)
{
if
(
res
.
res
.
statusCode
==
200
)
{
// console.log('upload success: %j', JSON.stringify(res));
// console.log('upload success: %j', JSON.stringify(res));
this
.
currentCheckpoint
=
null
;
this
.
currentCheckpoint
=
null
;
this
.
retryCount
=
0
this
.
retryCount
=
0
;
callback
(
common
.
genCallBacks
(
'FILE_UPLOAD_OSS_SUCCESS'
,
{
callback
(
errFromOSS
:
res
common
.
genCallBacks
(
"FILE_UPLOAD_OSS_SUCCESS"
,
{
}))
errFromOSS
:
res
,
})
);
}
else
{
}
else
{
callback
(
common
.
genCallBacks
(
'FILE_UPLOAD_OSS_FAIL'
,
{
callback
(
common
.
genCallBacks
(
"FILE_UPLOAD_OSS_FAIL"
,
{
errMsg
:
`OSS返回错误 状态码:
${
res
.
status
}
`
,
errMsg
:
`OSS返回错误 状态码:
${
res
.
status
}
`
,
errFromOSS
:
res
errFromOSS
:
res
,
}))
})
);
}
}
})
}).
catch
(
err
=>
{
.
catch
((
err
)
=>
{
console
.
error
(
err
);
console
.
error
(
err
);
console
.
log
(
`err.name :
${
err
.
name
}
`
);
console
.
log
(
`err.name :
${
err
.
name
}
`
);
console
.
log
(
`err.message :
${
err
.
message
}
`
);
console
.
log
(
`err.message :
${
err
.
message
}
`
);
...
@@ -164,49 +182,63 @@ class OSSUpLoader {
...
@@ -164,49 +182,63 @@ class OSSUpLoader {
console
.
error
(
`retryCount :
${
this
.
retryCount
}
`
);
console
.
error
(
`retryCount :
${
this
.
retryCount
}
`
);
setTimeout
(()
=>
{
setTimeout
(()
=>
{
this
.
_upload
(
fileName
,
file
,
callback
);
this
.
_upload
(
fileName
,
file
,
callback
);
},
this
.
retryInterval
)
},
this
.
retryInterval
);
}
else
{
}
else
{
callback
(
common
.
genCallBacks
(
'FILE_UPLOAD_OSS_TIMEOUT_OR_OTHER'
,
{
callback
(
common
.
genCallBacks
(
"FILE_UPLOAD_OSS_TIMEOUT_OR_OTHER"
,
{
errMsg
:
err
.
message
,
errMsg
:
err
.
message
,
err
err
,
}))
})
);
}
}
// }
// }
})
});
}
}
/**将文件路径转成文件对象
/**
* @param {Object} filePath 文件路径
* 将文件路径转换为可下载的文件对象
* @param {Object} callback 回调函数
*
* @param {string} filePath - 文件的路径
* @param {Function} callback - 转换完成时调用的回调函数
*
* @throws {Error} 如果请求过程中发生错误,将抛出错误
*/
*/
_pathToFile
(
filePath
,
callback
)
{
_pathToFile
(
filePath
,
callback
)
{
window
.
webkitURL
=
window
.
webkitURL
||
window
.
URL
;
window
.
webkitURL
=
window
.
webkitURL
||
window
.
URL
;
var
xhr
=
new
XMLHttpRequest
();
var
xhr
=
new
XMLHttpRequest
();
xhr
.
open
(
"GET"
,
filePath
,
true
);
xhr
.
open
(
"GET"
,
filePath
,
true
);
xhr
.
responseType
=
"blob"
;
xhr
.
responseType
=
"blob"
;
xhr
.
onload
=
function
()
{
xhr
.
onload
=
function
()
{
if
(
this
.
status
==
200
)
{
if
(
this
.
status
==
200
)
{
var
file
=
this
.
response
var
file
=
this
.
response
;
callback
(
common
.
genCallBacks
(
'FILE_PATH_TRANSFORM_SUCCESS'
,
{
callback
(
file
common
.
genCallBacks
(
"FILE_PATH_TRANSFORM_SUCCESS"
,
{
}))
file
,
})
);
}
else
{
}
else
{
callback
(
common
.
genCallBacks
(
'FILE_PATH_TRANSFORM_FAIL'
))
callback
(
common
.
genCallBacks
(
"FILE_PATH_TRANSFORM_FAIL"
));
}
}
};
};
xhr
.
onerror
=
function
(
errType
,
errMsg
)
{
xhr
.
onerror
=
function
(
errType
,
errMsg
)
{
callback
(
common
.
genCallBacks
(
'FILE_PATH_REQUEST_FAIL'
,
{
callback
(
common
.
genCallBacks
(
"FILE_PATH_REQUEST_FAIL"
,
{
errType
,
errType
,
errMsg
errMsg
,
}))
})
);
};
};
xhr
.
send
();
xhr
.
send
();
}
}
destroy
(){
/**
this
.
retryCount
=
0
* 销毁当前实例对象,释放资源并重置状态
this
.
client
=
null
* 该方法将重置上传器的重试次数为 0,并将客户端实例置为 null
*/
destroy
()
{
this
.
retryCount
=
0
;
this
.
client
=
null
;
}
}
}
;
}
const
common
=
{
const
common
=
{
constants
:
{
constants
:
{
...
@@ -216,28 +248,31 @@ const common = {
...
@@ -216,28 +248,31 @@ const common = {
FILE_PATH_TRANSFORM_FAIL
:
2
,
FILE_PATH_TRANSFORM_FAIL
:
2
,
FILE_UPLOAD_OSS_SUCCESS
:
0
,
FILE_UPLOAD_OSS_SUCCESS
:
0
,
FILE_UPLOAD_OSS_FAIL
:
3
,
FILE_UPLOAD_OSS_FAIL
:
3
,
FILE_UPLOAD_OSS_TIMEOUT_OR_OTHER
:
4
FILE_UPLOAD_OSS_TIMEOUT_OR_OTHER
:
4
,
},
},
MESSAGE
:
{
MESSAGE
:
{
FILE_PATH_TRANSFORM_SUCCESS
:
'文件转换成功'
,
FILE_PATH_TRANSFORM_SUCCESS
:
"文件转换成功"
,
FILE_PATH_REQUEST_FAIL
:
'文件转换请求失败'
,
FILE_PATH_REQUEST_FAIL
:
"文件转换请求失败"
,
FILE_PATH_TRANSFORM_FAIL
:
'文件转换失败'
,
FILE_PATH_TRANSFORM_FAIL
:
"文件转换失败"
,
FILE_UPLOAD_OSS_SUCCESS
:
'文件上传至OSS成功'
,
FILE_UPLOAD_OSS_SUCCESS
:
"文件上传至OSS成功"
,
FILE_UPLOAD_OSS_FAIL
:
'文件上传至OSS失败'
,
FILE_UPLOAD_OSS_FAIL
:
"文件上传至OSS失败"
,
FILE_UPLOAD_OSS_TIMEOUT_OR_OTHER
:
'文件上传至OSS超时或其他错误'
,
FILE_UPLOAD_OSS_TIMEOUT_OR_OTHER
:
"文件上传至OSS超时或其他错误"
,
FILE_PART_SIZE_TOO_SMALL
:
'分片大小不能小于100kb'
FILE_PART_SIZE_TOO_SMALL
:
"分片大小不能小于100kb"
,
}
},
},
},
/**
* 生成回调对象
*
* @param {string} key -回调键值
* @param {Object} data - 回调数据对象
* @return {Object} 回调对象
*/
genCallBacks
(
key
,
data
=
{})
{
genCallBacks
(
key
,
data
=
{})
{
const
{
const
{
CODE
,
MESSAGE
}
=
common
.
constants
;
CODE
,
MESSAGE
}
=
common
.
constants
return
{
return
{
code
:
CODE
[
key
],
code
:
CODE
[
key
],
message
:
MESSAGE
[
key
],
message
:
MESSAGE
[
key
],
data
}
}
}
\ No newline at end of file
\ No newline at end of file
data
,
};
},
};
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