当前位置: 首页 > 工具软件 > go-fastdfs > 使用案例 >

go-fastdfs client

尹凌龙
2023-12-01

分享一个go-fastdfs client

  • 调用函数
package utils

import (
	"github.com/astaxie/beego/httplib"
)
type FastDFSClient struct {
	Req *httplib.BeegoHTTPRequest
}
func NewFastDFSClient(serverUrl string)*FastDFSClient{
	client:=new(FastDFSClient)
	client.Req= httplib.Post(serverUrl)
	return client
}

func (this *FastDFSClient) Upload(filePath string)(info map[string]interface{},err error)  {
	info=make(map[string]interface{},0)
	//clientReq:=httplib.Post(this.Url)
	// only fill the file path value
	this.Req.PostFile("file",filePath)
	this.Req.Param("output","json")
	this.Req.Param("scene","")
	this.Req.Param("path","")
	err=this.Req.ToJSON(&info)
	return
}


  • 单例模式
package common_use

import (
	"api_quant_manage_system/utils"
	"github.com/spf13/viper"
	"sync"
)

var fastDfsClient *utils.FastDFSClient
func InitFastDFSClient(viper *viper.Viper){
	if fastDfsClient==nil{
		one:=sync.Once{}
		one.Do(func() {
			if fastDfsClient==nil{
				fastDfsClient=utils.NewFastDFSClient(viper.GetString("server_addr"))
			}
		})
	}
}


func GetFastDfsClient()*utils.FastDFSClient{
	return fastDfsClient
}
 类似资料: