赞
踩
package qiniu import ( "context" "io" "github.com/qiniu/api.v7/v7/auth/qbox" "github.com/qiniu/api.v7/v7/storage" ) const ( accessKey = "" secretKey = "" bucket = "" ) func config() (cfg storage.Config) { cfg = storage.Config{} // 设置机房 cfg.Zone = &storage.ZoneHuanan // 是否使用https cfg.UseHTTPS = false // 是否使用CDN上传加速 cfg.UseCdnDomains = false return } func Upload(file io.Reader, filename string, size int64) (string, error) { putPolicy := storage.PutPolicy{ Scope: bucket, } mac := qbox.NewMac(accessKey, secretKey) upToken := putPolicy.UploadToken(mac) cig := config() formUploader := storage.NewFormUploader(&cig) ret := storage.PutRet{} putExtra := storage.PutExtra{} err := formUploader.Put(context.Background(), &ret, upToken, filename, file, size, &putExtra) if err != nil { return "", err } return ret.Key, nil }
f, h, err := this.GetFile("audio") if err != nil { this.renderUnknownErrorJSON("上传失败", nil) return } defer f.Close() file, _ := h.Open() // 返回值一个接口类型,内部包含了io.Reader接口 // 通过时间戳作为文件名 unix := time.Now().Unix() m := md5.New() io.WriteString(m, strconv.FormatInt(unix, 10)) filename := fmt.Sprintf("%x", md5.Sum(nil)) if filename, err = qiniu.Upload(file, filename+".mp3", h.Size); err != nil { // 通过h.size 即可获得文件大小 log.Printf("controllers:work ----》 err:%v", err) this.renderJSON(UPLOAD_FAILED, "上传失败", nil) return }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。