golang版微信小程序图片上传,服务器保存

golang版微信小程序图片上传,服务器保存

  1. 其实很简单,小程序那边调wx.uploadFile其实就是客户端发起一个 HTTPS POST 请求,其中 content-type 为 multipart/form-data,只不过以前是自己写接口,现在是由微信的api来调用.

  2. 怎么读取呢,你只需要读取FromFile表单中的内容就行了,go很简单不用框架,我为了方便开发用的gin

    file, header, err := c.Request.FormFile("file")
    

    完整代码

    func HandleUploadWxFile(c *gin.Context) {
           
    defer c.Request.Body.Close()
    file, header, err := c.Request.FormFile("file")
    if err != nil {
           
    	tools.Mlog.Error(err.Error())
    	c.JSON(http.StatusInternalServerError, err.Error())
    	return
    }
    //文件流就在file里
    defer file.Close()
    source := c.PostForm("source")
    contentType := header.Header.Get("Content-Type")
    name := fmt.Sprintf("%s.%s", source, strings.Split(contentType, "/")[1])
    objKey, err := UploadFileWithReader(//阿里云上传部分
    	reqUpdateWXFile{
           Name: name, File: file, FileCategory: contentType, Source: source})
    if err != nil {
           
    	if err == ErrSupportFileType {
           
    		tools.Mlog.Error(err.Error())
    		c.JSON(http.StatusOK, code.GeneralRetInvalidParams(err.Error()))
    	} else {
           
    		tools.Mlog.Error(err.Error())
    		c.JSON(http.StatusInternalServerError, err.Error())
    	}
    	return
    }
    c.JSON(http.StatusOK, gin.H{
           
    	"link": aliyun.GeneralObjectUrl(objKey),
    })
    }
    
  3. 最后附上阿里云上传代码

    初始化阿里云

    func createOSS() (*oss.Client, error) {
           
    client, err := oss.New(endPoint, setting.AliyunCloudConf.AccessKeyID, setting.AliyunCloudConf.AccessKeySecret,
    	func(c *oss.Client) {
           
    		c.Config.IsDebug = setting.IsDebug
    		c.Config.HTTPTimeout = oss.HTTPTimeout{
           
    			IdleConnTimeout:  90 * time.Second,
    			ConnectTimeout:   30 * time.Second,
    			ReadWriteTimeout: 60 * time.Second,
    			HeaderTimeout:    60 * time.Second,
    			LongTimeout:      300 * time.Second,
    		}
    	})
    if err != nil {
           
    	err := fmt.Errorf("aliyun cloud start failed, err: %s", err.Error())
    	//tools.Mlog.Error(err.Error())
    	return nil, err
    }
    return client, nil
    }
    
    func PutObject(objKey string, reader io.Reader, options ...oss.Option) error {
           
    client, err := createOSS()
    if err != nil {
           
    	return err
    }
    bucket, err := client.Bucket(bucketName)
    if err != nil {
           
    	return err
    }
    return bucket.PutObject(objKey, reader, options...)
    }
    
    
    func UploadFileWithReader(info reqUpdateWXFile) (objKey string, err error) {
           
    intn := rand.Intn(10000)
    if info.Source == aliyun.Avatar {
           
    	objKey = fmt.Sprintf("%s/%d_%d%s", aliyun.Avatar, time.Now().UTC().Unix(), intn, info.Name)
    } else if info.Source == aliyun.Photo {
           
    	objKey = fmt.Sprintf("%s/%d_%d%s", aliyun.Photo, time.Now().UTC().Unix(), intn, info.Name)
    } else if info.Source == aliyun.Video {
           
    	objKey = fmt.Sprintf("%s/%d_%d%s", aliyun.Video, time.Now().UTC().Unix(), intn, info.Name)
    } else if info.Source == aliyun.Other {
           
    	objKey = fmt.Sprintf("%s/%d_%d%s", aliyun.Other, time.Now().UTC().Unix(), intn, info.Name)
    } else {
           
    	return "", ErrSupportFileType
    }
    
    err = aliyun.PutObject(objKey, info.File, oss.ObjectACL(oss.ACLPublicRead), oss.ContentType(info.FileCategory))
    if err != nil {
           
    	return "", err
    }
    return
    }
    

你可能感兴趣的:(golang,小程序,图片上传)