Article directory
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.
- Find the storage space for resource management
- Create space
-
New storage space
-
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.
-
Just bind the domain name
Just bind your domain name to it.
If there is no domain name, here is a domain name sent by Qiniu
- get key
- Both of these
That'll be fine
2. config
- config.ini file
AccessKey=是上面AK
SercetKey=是上面的SK
Bucket=是刚刚新创的名称!
QiniuServer=是绑定的域名
- 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.go
Create a file under utils under the pkg package
- 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
}