tus-node-server 官方基于node 实现的tus server 1.0 发布
tus-node-server 是基于typescript 开发的新的nodejs server,通过拆分为独立的包(file,s3,gcs, server )
包含的特性
- 完全基于typescript 开发
- 拆分为了独立的包
- 提供了新的events 以及hooks
- 升级了依赖包
- 重新设计了存储
- file 存储添加了过去扩展
- 更好的周边集成(express,fastify。。。)
参考使用
- s3
version: '3'
services:
minio:
image: minio/minio:RELEASE.2023-09-16T01-01-47Z
ports:
- "9000:9000"
- "19001:19001"
environment:
MINIO_ACCESS_KEY: minio
MINIO_SECRET_KEY: minio123
command: server --console-address :19001 --quiet /data
- app.js
const port = 1080
const datastore = new S3Store({
partSize: 8 * 1024 * 1024, // 8MB
s3ClientConfig: {
bucket: process.env.AWS_BUCKET,
region: process.env.AWS_REGION,
endpoint: process.env.AWS_ENDPOINT,
tls: process.env.AWS_TLS === 'true',
credentials: {
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
},
forcePathStyle: true,
},
})
const server = new Server({
path: '/files',
datastore,
})
// New events system
server.on(EVENTS.POST_CREATE, (req, res, upload) => {
console.log('POST_CREATE',upload)
})
server.on(EVENTS.POST_RECEIVE, (req, res, upload) => {
console.log('POST_RECEIVE',upload)
})
server.on(EVENTS.POST_FINISH, (req, res, upload) => {
console.log('POST_FINISH',upload)
})
server.on(EVENTS.POST_TERMINATE, (req, res, id) => {
console.log('POST_TERMINATE',id)
})
server.listen({ host, port })
- client.js
const { Server, EVENTS } = require('@tus/server')
const { S3Store } = require('@tus/s3-store')
require('dotenv').config()
const host = '0.0.0.0'
const fs = require('fs')
const tus = require('tus-js-client')
const path = `${__dirname}/my.mp4`
const file = fs.createReadStream(path)
const options = {
endpoint: 'http://localhost:1080/files/',
metadata: {
filename: 'my.mp4',
filetype: 'video/mp4',
},
onError(error) {
console.error('An error occurred:')
console.error(error)
process.exitCode = 1
},
onProgress(bytesUploaded, bytesTotal) {
const percentage = ((bytesUploaded / bytesTotal) * 100).toFixed(2)
console.log(bytesUploaded, bytesTotal, `${percentage}%`)
},
onSuccess() {
console.log('Upload finished:', upload.url)
},
}
const upload = new tus.Upload(file, options)
upload.start()
- 效果
说明
目前来说tus-node-server 做为一个支持断点续传的解决方案还是很不错的,一个参考集成
对于客户端可以使用提供的周边工具,我们可以基于tus 的node server 或者golang 也是可以的,进行自己的定制调整
可以实现大文件的断点续传能力,node server 提供的扩展能力还是很不错的
参考资料
https://github.com/tus/tus-node-server
https://tus.io/blog/2023/09/04/tus-node-server-v100
https://github.com/tus/tus-js-client/blob/main/docs/api.md
https://github.com/tus/tus-node-server/tree/main/packages/server#servergetreq-res
https://github.com/tus/tus-node-server/issues/483