安装第三方包
go get github.com/tus/tusd/v2
package main
import (
"fmt"
"net/http"
"github.com/tus/tusd/v2/pkg/filestore"
tusd "github.com/tus/tusd/v2/pkg/handler"
)
func main() {
// 本地磁盘
store := filestore.FileStore{
Path: "./uploads",
}
//文件存储
composer := tusd.NewStoreComposer()
store.UseIn(composer)
//定义handler
handler, err := tusd.NewHandler(tusd.Config{
BasePath: "/files/",
StoreComposer: composer,
MaxSize: 1000000000, // 1GB
Resumable: true, //开启断点续传
Resume: true,
NotifyCompleteUploads: true, //通知
})
if err != nil {
panic(fmt.Errorf("unable to create handler: %s", err))
}
//监听上传
go func() {
for {
event := <-handler.CompleteUploads
fmt.Printf("Upload %s finished\n", event.Upload.ID)
}
}()
// 上传地址 http://localhost:8080/files
http.Handle("/files/", http.StripPrefix("/files/", handler))
err = http.ListenAndServe(":8080", nil)
if err != nil {
panic(fmt.Errorf("unable to listen: %s", err))
}
}
标签:断点续传,tus,tusd,v2,handler,composer,Go
From: https://www.cnblogs.com/qcy-blog/p/18110482