当前位置:网站首页>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 !
边栏推荐
- Analysis and solution of application jar package conflict in yarn environment
- HCIA OSPF
- IP SAN拥有独立的文件系统,应用服务器通过网络共享协议访问到IP SAN后,可以对文件系统中的文件进行读写操作
- 基于网络编码的卫星网络容量提升方法
- [Acwing]第 60 场周赛 B- 4495. 数组操作
- 基于“7·20郑州特大暴雨”对空天地一体化通信的思考
- HCIA static basic experiment 7.8
- Custom complex logic verification during adding and modifying -2022 new project
- Bazel use tutorial to
- "Baidu side" angrily sprayed the interviewer! Isn't it that the tree time increases by a line number?
猜你喜欢
随机推荐
【华为云IoT】读书笔记之《万物互联:物联网核心技术与安全》第3章(下)
Map遍历 key-value 的4种方法
欧拉角,轴角,四元数与旋转矩阵详解
ROS 重名
双向NAT技术
Bidirectional NAT Technology
反向散射通信的未来应用与技术挑战
Transplant Wu Enda's deep learning 01 machine learning and neural network second week neural network basic programming homework elective homework to pycharm
[PostgreSQL] PostgreSQL 15 optimizes distinct
从“被动”到“主动”,ZETA技术助力“RFID2.0”升级该如何实现?
常见集合特性
王者荣耀商城异地多活架构设计
自动化之图形界面库pyautogui
Custom complex logic verification during adding and modifying -2022 new project
看一看这丑恶嘴脸 | MathWorks Account Unavailable - Technical Issue
LeetCode 2331. 计算布尔二叉树的值(树的遍历)
About hping streaming test tool
Leetcode ugly number problem solution
HCIA OSPF
从预测到决策,九章云极DataCanvas推出YLearn因果学习开源项目









