@author: [piexlmax](https://github.com/piexlmax) @author: [ccfish86](https://github.com/ccfish86) @author: [SliverHorn](https://github.com/SliverHorn) @object: *Qiniu @function: UploadFile @description: 上传文件 @param: file *multipart.FileHeader @return: string, string, error
(file *multipart.FileHeader)
| 25 | //@return: string, string, error |
| 26 | |
| 27 | func (*Qiniu) UploadFile(file *multipart.FileHeader) (string, string, error) { |
| 28 | putPolicy := storage.PutPolicy{Scope: global.GVA_CONFIG.Qiniu.Bucket} |
| 29 | mac := qbox.NewMac(global.GVA_CONFIG.Qiniu.AccessKey, global.GVA_CONFIG.Qiniu.SecretKey) |
| 30 | upToken := putPolicy.UploadToken(mac) |
| 31 | cfg := qiniuConfig() |
| 32 | formUploader := storage.NewFormUploader(cfg) |
| 33 | ret := storage.PutRet{} |
| 34 | putExtra := storage.PutExtra{Params: map[string]string{"x:name": "github logo"}} |
| 35 | |
| 36 | f, openError := file.Open() |
| 37 | if openError != nil { |
| 38 | global.GVA_LOG.Error("function file.Open() failed", zap.Any("err", openError.Error())) |
| 39 | |
| 40 | return "", "", errors.New("function file.Open() failed, err:" + openError.Error()) |
| 41 | } |
| 42 | defer f.Close() // 创建文件 defer 关闭 |
| 43 | fileKey := fmt.Sprintf("%d%s", time.Now().Unix(), file.Filename) // 文件名格式 自己可以改 建议保证唯一性 |
| 44 | putErr := formUploader.Put(context.Background(), &ret, upToken, fileKey, f, file.Size, &putExtra) |
| 45 | if putErr != nil { |
| 46 | global.GVA_LOG.Error("function formUploader.Put() failed", zap.Any("err", putErr.Error())) |
| 47 | return "", "", errors.New("function formUploader.Put() failed, err:" + putErr.Error()) |
| 48 | } |
| 49 | return global.GVA_CONFIG.Qiniu.ImgPath + "/" + ret.Key, ret.Key, nil |
| 50 | } |
| 51 | |
| 52 | //@author: [piexlmax](https://github.com/piexlmax) |
| 53 | //@author: [ccfish86](https://github.com/ccfish86) |
nothing calls this directly
no test coverage detected