123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- package main
-
- import (
- "context"
- "fmt"
- "gdyb/models"
- _ "gdyb/routers"
- "gdyb/service"
- "github.com/astaxie/beego"
- "github.com/qiniu/api.v7/auth/qbox"
- "github.com/qiniu/api.v7/storage"
- "os"
- "strconv"
- "time"
- )
-
- func init() {
- service.ConnectDB()
- //org_id, _ := beego.AppConfig.Int64("org_id")
- //miConfig, _ := service.FindMedicalInsuranceInfo(org_id)
- //CreateLog(miConfig)
- //UploadLog(miConfig)
- }
- func main() {
- beego.Run()
- }
-
- func CreateLog(miConfig models.MedicalInsuranceOrgConfig) {
- Mkdir(miConfig.OrgName + "日志")
- //org_id,_ := beego.AppConfig.Int("org_id")
- month := time.Unix(1557042972, 0).Format("1")
- year := time.Now().Format("2006")
- month = time.Now().Format("01")
- day := time.Now().Format("02")
- file := strconv.FormatInt(miConfig.UserOrgId, 10) + "_" + year + month + day + "_log"
- file_name := file + ".txt"
- file_path := miConfig.OrgName + "日志" + "/" + file_name
- exist, _ := PathExists(file_path)
- if exist { //存在
- fmt.Println("存在")
- f, err := os.OpenFile(file_path, os.O_WRONLY, 0644)
- if err != nil {
- fmt.Println("read fail")
- }
- content := "\r\r\r"
- n, _ := f.Seek(0, 2)
- _, err = f.WriteAt([]byte(content), n)
-
- } else { //不存在
- fmt.Println("文件不存在,创建文件")
- f, err := os.Create(miConfig.OrgName + "日志" + "/" + file_name)
- defer f.Close()
- if err != nil {
- } else {
- _, err = f.Write([]byte("记录日志"))
- }
- }
-
- }
- func UploadLog(miConfig models.MedicalInsuranceOrgConfig) {
- //获取七牛云上的文件信息,不存在则表示没上传到七牛云,上传昨天日志信息
- nTime := time.Now()
- yesTime := nTime.AddDate(0, 0, -1)
- file_key := strconv.FormatInt(miConfig.UserOrgId, 10) + "_" + yesTime.Format("20060102") + "_log.txt"
- mac := qbox.NewMac(beego.AppConfig.String("qiniu_accesskey"), beego.AppConfig.String("qiniu_secretkey"))
- cfg := storage.Config{
- // 是否使用https域名进行资源管理
- UseHTTPS: true,
- }
- // 指定空间所在的区域,如果不指定将自动探测
- // 如果没有特殊需求,默认不需要指定
- //cfg.Zone=&storage.ZoneHuabei
- bucketManager := storage.NewBucketManager(mac, &cfg)
- bucket := beego.AppConfig.String("qiniu_bucket")
- key := file_key
- file, sErr := bucketManager.Stat(bucket, key)
- fmt.Println(file)
- fmt.Println(sErr)
- fmt.Println(miConfig.OrgName + "日志" + "/" + file_key)
-
- if sErr != nil {
- fmt.Println("七牛云上文件不存在,上传到七牛云")
- //判断本地文件是否存在,存在则执行上传操作
- exist, _ := PathExists(miConfig.OrgName + "日志" + "/" + file_key)
- if exist {
- upload_qiniu(miConfig.OrgName+"日志"+"/"+file_key, file_key)
- } else {
- fmt.Println("本地文件不存在")
-
- }
- } else {
- fmt.Println("七牛云文件存在")
- }
-
- }
- func Mkdir(dir string) {
- // 创建文件夹
- exist, err := PathExists(dir)
- if err != nil {
- fmt.Println(err.Error())
- } else {
- if exist {
- fmt.Println(dir + "文件夹已存在!")
- } else {
- // 文件夹名称,权限
- err := os.Mkdir(dir, os.ModePerm)
- if err != nil {
- fmt.Println(dir+"文件夹创建失败:", err.Error())
- } else {
- fmt.Println(dir + "文件夹创建成功!")
- }
- }
- }
- }
- func PathExists(path string) (bool, error) {
- _, err := os.Stat(path)
- if err == nil {
- return true, nil
- }
- if os.IsNotExist(err) {
- return false, nil
- }
- return false, err
- }
- func upload_qiniu(filePath string, keys string) {
- key := keys
- //上传凭证,关于凭证这块大家可以去看看官方文档
- putPolicy := storage.PutPolicy{
- Scope: beego.AppConfig.String("qiniu_bucket"),
- }
- mac := qbox.NewMac(beego.AppConfig.String("qiniu_accesskey"), beego.AppConfig.String("qiniu_secretkey"))
- upToken := putPolicy.UploadToken(mac)
- cfg := storage.Config{}
- //空间对应机房
- //其中关于Zone对象和机房的关系如下:
- // 机房 Zone对象
- // 华东 storage.ZoneHuadong
- // 华北 storage.ZoneHuabei
- // 华南 storage.ZoneHuanan
- // 北美 storage.ZoneBeimei
- //七牛云存储空间设置首页有存储区域
- cfg.Zone = &storage.ZoneHuadong
- //不启用HTTPS域名
- cfg.UseHTTPS = false
- //不使用CND加速
- cfg.UseCdnDomains = false
- //构建上传表单对象
- formUploader := storage.NewFormUploader(&cfg)
- ret := storage.PutRet{}
- // 可选
- putExtra := storage.PutExtra{
- Params: map[string]string{},
- }
- err := formUploader.PutFile(context.Background(), &ret, upToken, key, filePath, &putExtra)
- if err != nil {
- fmt.Println(err)
- return
- }
- fmt.Println(ret.Key, ret.Hash)
- }
|