当前位置:网站首页>Beego framework realizes file upload + seven cattle cloud storage
Beego framework realizes file upload + seven cattle cloud storage
2022-07-19 10:45:00 【Try to hit the keyboard ing】
One 、 download
stay go.mod Introduction in
github.com/qiniu/api.v7/v7 v7.8.2Introduce... Into the controller
"github.com/qiniu/api.v7/v7/auth/qbox"
"github.com/qiniu/api.v7/v7/storage"A warning will appear when using this

But it will not affect the upload
Two 、 Use steps ( Just beginning to learn go, All is to save the picture locally first , Then upload )
1. Save to local
fileSuffix := ".png"
rand.Seed(time.Now().UnixNano())
randNum := fmt.Sprintf("%d", rand.Intn(9999)+1000)
hashName := md5.Sum([]byte(time.Now().Format("2006_01_02_15_04_05_") + randNum))
fileName := fmt.Sprintf("%x", hashName) + fileSuffix
imageUrl := "static/img/" + fileName
err := u.SaveToFile("image", imageUrl)
if err != nil {
u.Ctx.WriteString(fmt.Sprintf("%v", err))
return
}
u.Ctx.WriteString(" Upload local successfully ~!!!!!!!")
u.Ctx.WriteString(imageUrl)The location to save the local is static/img, If not, please create .
2. Cloud storage ( Here is the manual of seven cattle cloud )
The code is as follows :
bucket, _ := beego.AppConfig.String("bucket")
putPolicy := storage.PutPolicy{
Scope: bucket,
}
accessKey, _ := beego.AppConfig.String("accessKey")
secretKey, _ := beego.AppConfig.String("secretKey")
mac := qbox.NewMac(accessKey, secretKey)
upToken := putPolicy.UploadToken(mac)
cfg := storage.Config{}
// The computer room corresponding to the space
cfg.Zone = &storage.ZoneHuadong
// Whether to use https domain name
cfg.UseHTTPS = true
// Upload whether to use CDN Upload acceleration
cfg.UseCdnDomains = true
// Build the object that the form Uploads
formUploader := storage.NewFormUploader(&cfg)
ret := storage.PutRet{}
// Optional configuration
putExtra := storage.PutExtra{
Params: map[string]string{
"x:name": "github logo",
},
}
localFile := imageUrl
//imageUrl:static/img/7bc0954e8edfc48895e7c33c280a66d3.png
key := "shopping/userinfo/" + fileName // File location after storage ,shopping Folder userinfo Folder filename File name
err = formUploader.PutFile(context.Background(), &ret, upToken, key, localFile, &putExtra)
if err != nil {
fmt.Println(err)
return
}
u.Ctx.WriteString(" Successful cloud storage ")key: It refers to the location where the image is stored after uploading to the space
localFile: Indicates the address of the file to be uploaded
here “ Build the object that the form Uploads ” Not very well understood , I'll add it later .
There are better methods to supplement later , I hope the big guys can give advice !
边栏推荐
猜你喜欢

Bidirectional NAT Technology

LeetCode 2319. 判断矩阵是否是一个 X 矩阵

ROS 重名

如何使用SVG制作沿任意路径排布的文字效果

爱可可AI前沿推介(7.17)

"Baidu side" angrily sprayed the interviewer! Isn't it that the tree time increases by a line number?

OpenCV编程:OpenCV3.X训练自己的分类器

Virtual CPU and memory in yarn (CDH)

Design and Simulation of intelligent storage cabinet control system
![[LeetCode周赛复盘] 第 302 场周赛20220717](/img/38/446db9b4755f8b30f9887faede7e95.png)
[LeetCode周赛复盘] 第 302 场周赛20220717
随机推荐
基于网络编码的卫星网络容量提升方法
博弈论(depu)与投资(40/100)
HCIA OSPF
欧拉角,轴角,四元数与旋转矩阵详解
Autojs learning - Dynamic decryption
Develop the first Flink app
【PostgreSQL 】PostgreSQL 15对distinct的优化
Design of the multi live architecture in different places of the king glory mall
TS解决引入插件的类型文件不存在的问题
6G智慧内生:技术挑战、架构和关键特征
Pytorch手动实现多层感知机
[Acwing] 第 60 场周赛 C-AcWing 4496. 吃水果
追根问底:Objective-C关联属性原理分析
The R language uses the plot function in the native package (basic import package, graphics) to visualize the scatter plot
If you use mybatics to access Damon database, is it exactly the same? Because the SQL syntax has not changed. Right?
分类任务中的类别不平衡问题
[PostgreSQL] PostgreSQL 15 optimizes distinct
Detailed explanation of C language custom types
Design and Simulation of intelligent storage cabinet control system
查找——平衡二叉树