【Go actual combat | E-commerce platform】(7) Upload the picture to Qiniuyun

In this chapter, we introduce how to upload images to Qiniu Cloud and return the url of the corresponding image. Because the subsequent creation of our products, replacement of avatars, etc., all upload our pictures to Qiniu cloud storage, and then return the corresponding url.

1. Get configuration information

  • Open the Qiniuyun official website and enter the console.
    insert image description here
  • Find the storage space for resource management

insert image description here

  • Create space

insert image description here

  • New storage space
    insert image description here

  • Then we enter this page, you can choose to bind the domain name, or you can choose not to bind. If you don't bind Qiniu, you will be given a domain name to experience, which seems to be valid for 7 days.
    insert image description here

  • Just bind the domain name

insert image description here
Just bind your domain name to it.
insert image description here
If there is no domain name, here is a domain name sent by Qiniu

insert image description here

  • get key
    insert image description here
  • Both of these
    insert image description here

That'll be fine

2. config

  • config.ini file
AccessKey=是上面AK
SercetKey=是上面的SK
Bucket=是刚刚新创的名称!
QiniuServer=是绑定的域名

insert image description here

  • conf.go file

load configuration

func LoadQiniu(file *ini.File) {
    
    
	AccessKey = file.Section("qiniu").Key("AccessKey").String()
	SerectKey = file.Section("qiniu").Key("SerectKey").String()
	Bucket = file.Section("qiniu").Key("Bucket").String()
	QiniuServer = file.Section("qiniu").Key("QiniuServer").String()
}

3. upload

upload.goCreate a file under utils under the pkg package

insert image description here

  • read configuration file
	var AccessKey = conf.AccessKey
	var SerectKey = conf.SerectKey
	var Bucket = conf.Bucket
	var ImgUrl = conf.QiniuServer
	putPlicy := storage.PutPolicy{
    
    
		Scope:Bucket,
	}
  • Upload Qiniu cloud and return url and status
	mac := qbox.NewMac(AccessKey,SerectKey)
	upToken := putPlicy.UploadToken(mac)
	cfg := storage.Config{
    
    
		Zone : &storage.ZoneHuanan,
		UseCdnDomains : false,
		UseHTTPS : false,
	}
	putExtra := storage.PutExtra{
    
    }
	formUploader := storage.NewFormUploader(&cfg)
	ret := storage.PutRet{
    
    }
	err := formUploader.PutWithoutKey(context.Background(),&ret,upToken,file,fileSize,&putExtra)
	if err != nil {
    
    
		code := e.ErrorUploadFile
		return code , err.Error()
	}
	url := ImgUrl + ret.Key
	return 200,url

full code

The incoming file, as well as the file size, returns the url and status

package util

import (
	"context"
	"github.com/qiniu/go-sdk/v7/auth/qbox"
	"github.com/qiniu/go-sdk/v7/storage"
	"mall/conf"
	"mall/pkg/e"
	"mime/multipart"
)

// 封装上传图片到七牛云然后返回状态和图片的url
func UploadToQiNiu(file multipart.File ,fileSize int64) (int,string) {
    
    
	var AccessKey = conf.AccessKey
	var SerectKey = conf.SerectKey
	var Bucket = conf.Bucket
	var ImgUrl = conf.QiniuServer
	putPlicy := storage.PutPolicy{
    
    
		Scope:Bucket,
	}
	mac := qbox.NewMac(AccessKey,SerectKey)
	upToken := putPlicy.UploadToken(mac)
	cfg := storage.Config{
    
    
		Zone : &storage.ZoneHuanan,
		UseCdnDomains : false,
		UseHTTPS : false,
	}
	putExtra := storage.PutExtra{
    
    }
	formUploader := storage.NewFormUploader(&cfg)
	ret := storage.PutRet{
    
    }
	err := formUploader.PutWithoutKey(context.Background(),&ret,upToken,file,fileSize,&putExtra)
	if err != nil {
    
    
		code := e.ErrorUploadFile
		return code , err.Error()
	}
	url := ImgUrl + ret.Key
	return 200,url
}

Guess you like

Origin blog.csdn.net/weixin_45304503/article/details/121499608