这个demo的目标是提供一个能断点上传大文件的组件,只要client端按照协商好的格式,上传数据,fuploadServer就能提供正确的上传服务。
准备好一个名字为test.mp4的文件,放在/home 下。
1、运行fuploaderServer
nohup ./fuploadServer -c ./conf/fupload.yml &
2、运行业务服务器
nohup ./businessServer &
3、运行client端
./client
可以看到会把/home/test.mp4上传到上传组件所在机器上的/tmp/business/目录下。
git clone https://github.com/gmluckly/fuploadServer
go build
配置conf中的fupload.yaml文件,
bServer项为与业务服务器交互的信息配置,配置token和业务服务回调url
启动:
nohup/fupload_server -c ./conf/fupload.yaml &
也可以使用Dockerfile打包成镜像,在docker中运行
docker build -t fuploadserver:v1.0.0 .
docker run --name fupload -d -p8090:8090 fuploadserver:v1.0.0
当我们以组件的形式提供服务的时候,我们把角色分成三个:
(1)客户端(client) ,上传文件的角色
(2)业务服务器(business server)
(3)上传组件(fuploadServer)
request:
method: post
content-Type:application/json
body:
{
"token": string, //业务服务与上传组件约定的token认证
"userId": string, //上传的用户id
"storePath": string, //最终存放的路径
"fileName": string, //文件名称
"fileSize": int64, //文件大小
"fileMd5": string, //文件的md5值
"feedBackParameter": interface{} //上传后的回调参数,业务服务器可以封装为任意值,上传组件不会解析
}
reponse:
{
"retCode": string //0成功,其他失败
"status": string //失败原因
"data":{
"taskId": int64 //上传id
"uploadUrl": string //上传文件块url
"blocksUrl": string //查询上传任务信息url,获取上传组件期待的文件块
"stateUrl":string //更新上传任务的url
}
}
例如,curl命令:
curl -i -H 'content-type: application/json' -X POST -d '{"token":"xxxxx","userId":"123","storePath":"/tmp/fupload/","fileName":"hello.mp4","fileSize":"345000000","fileMd5":"04465d416b4eca345976021db8698f15"}' http://ip:port/api/upload/new/task
http 请求,url为申请上传时返回的uploadUrl
request:
method: post,
content-Type: multiplepart/form-data
body:
{
partMd5: string //块MD5的值
startByte: int64 //文件块开始的位置(字节)
startByte: int64 //文件块结束的位置(字节)
file: binary //块内容
}
reponse:
{
"retCode": string //0成功,1上传组件提示需要某些块,其他失败
"status": string //失败原因
"data":{
startByte: int64 //开始位置
startByte: int64 //结束位置
}
}
例如,curl命令:
curl -i -H 'Content-Type:multipart/form-data' -X POST -F 'startByte=0' -F 'endByte=1000' -F 'partMd5=a01610228fe998f515a72dd730294d87' -F 'file=/tmp/1.mp4.01' http://ip:port/api/upload/files/block?taskId=123 // uploadUrl
http 请求,url为申请上传时返回的stateUrl
request:
method: post,
content-Type:application/json
body:
{
taskId: int64 //上传任务id
status: string //"pause"暂停,"upload" 上传,"cancel"取消
}
reponse:
{
"retCode": string //0成功,其他失败
"status": string //失败原因
"data": string
}
例如,curl命令:
curl -i -H 'content-type: application/json' -X POST -d '{"taskId":12344,"status":"pause"}' http://ip:port/api/upload/files/state //stateUrl
http 请求,url为申请上传时返回的blocksUrl
request:
method: get,
reponse:
{
totalSize: int64 //上传任务的大小
status: string //上传状态 "pause"暂停,"upload" 上传,"cancel"取消
expectBlocks: {
startByte: int64
endByte: int64
}
}
例如,curl命令:
curl http://ip:port/api/upload/files/blocks/info?taskId=123 //blocksUrl
http 请求,url为配置项中定义的notifyUrl
request:
method: post,
content-Type:application/json
body:
{
retCode: string //0成功,其他失败
status: string //失败原因
data: "" //申请上传时的回调参数,只有retCode为0 时返回
}
reponse:
忽略