当前位置:网站首页>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 !
边栏推荐
- 线程池原理
- Avi 部署使用指南(2):Avi 架构概述
- Use testeract JS offline recognition picture text record
- SAP ECC 和 S4HANA Material 物料库存管理的模型比较
- LeetCode 2335. 装满杯子需要的最短总时长
- 【PostgreSQL 】PostgreSQL 15对distinct的优化
- Leetcode ugly number problem solution
- 知其然,而知其所以然,JS 对象创建与继承
- ENVI_IDL:使用反距离权重法选取最近n个点插值(底层实现)并输出为Geotiff格式(效果等价于Arcgis中反距离权重插值)
- 二叉树刷题(二)
猜你喜欢
随机推荐
String类型函数传递问题
SAP AppGyver 的 Universal Theme System 使用介绍
JSP based novel writing and creation website
破案了卧槽---从MQ消费的逻辑怎么改代码都不生效
自动化之图形界面库pyautogui
Take a look at this ugly face | MathWorks account unavailable - technical issue
Map遍历 key-value 的4种方法
高数_第1章空间解析几何与向量代数__点到平面的距离
从预测到决策,九章云极DataCanvas推出YLearn因果学习开源项目
High number__ Relationship between equation and function
Autojs learning - multi function treasure chest - medium
antd表单设置数组字段
[Acwing] 第 60 场周赛 C-AcWing 4496. 吃水果
ROS 重名
电商销售数据分析与预测(日期数据统计、按天统计、按月统计)
win10开始键点击无响应
VScode+Unity3D的配置
知其然,而知其所以然,JS 对象创建与继承
Scala在Idea中的配量
High number_ Chapter 1 space analytic geometry and vector algebra__ Distance from point to plane









