//初始化七牛服务 func (this *QiniuService) InitQiniu() { config := revel.Config storage_type, ok := config.String("storage.type") this.isUsed = false if ok && storage_type == "qiniu" { var qiniu_AK string var qiniu_SK string qiniu_AK, ok = config.String("storage.qiniu.AK") if !ok || qiniu_AK == "" { return } qiniu_SK, ok = config.String("storage.qiniu.SK") if !ok || qiniu_SK == "" { return } this.qiniu_BaseUrl, ok = config.String("storage.qiniu.BaseUrl") if !ok || this.qiniu_BaseUrl == "" { return } this.qiniu_bucket, ok = config.String("storage.qiniu.bucket") if !ok || this.qiniu_bucket == "" { return } this.isUsed = true kodo.SetMac(qiniu_AK, qiniu_SK) zone := 0 c := kodo.New(zone, nil) this.bucket = c.Bucket(this.qiniu_bucket) this.ctx = context.Background() } }
func init() { kodo.SetMac(beego.AppConfig.String("access_key"), beego.AppConfig.String("secret_key")) zone := 0 // 您空间(Bucket)所在的区域 c := kodo.New(zone, nil) // 用默认配置创建 Client bucket = c.Bucket(beego.AppConfig.String("bucket_name")) fmt.Println("test") }
func NewQiniu(accessKey string, secretKey string) *Qiniu { kodo.SetMac(accessKey, secretKey) client := kodo.New(0, nil) q := &Qiniu{} q.client = client q.buckets = make(map[string]kodo.Bucket) return q }
func NewStorage(c *Config) *Storage { kodo.SetMac(c.AccessKey, c.SecretKey) zone := 0 client := kodo.New(zone, nil) bucket := client.Bucket(c.Bucket) return &Storage{Bucket: bucket} }
func getClient() { if client != nil { return } kodo.SetMac(AK, SK) zone := 0 // 您空间(Bucket)所在的区域 client = kodo.New(zone, nil) // 用默认配置创建 Client bucket = client.Bucket(BUCKETNAME) return }
// require the upload token func (this *ImageRespository) GetUPToken() string { kodo.SetMac(Access_Key, Secure_Key) zone := 0 c := kodo.New(zone, nil) bucket := "photoswall" policy := &kodo.PutPolicy{ Scope: bucket, Expires: 3600, } upToken := c.MakeUptoken(policy) return upToken }
//将本地文件上传到七牛云存储,返回文件在七牛上的路径。 func UploadToQiniu(fpLocal, fpOnQiNiu, bucket string) error { kodo.SetMac(QiNiuAccessKey, QiNiuSecretKey) zone := 0 // 空间(Bucket)所在的区域 c := kodo.New(zone, nil) // 用默认配置创建 Client b := c.Bucket(bucket) ctx := context.Background() err := b.PutFile(ctx, nil, fpOnQiNiu, fpLocal, nil) if err != nil { log.Println("上传文件到七牛云存储失败") return err } return nil }
func UpdateQiniu(fileName string, fileData string) bool { kodo.SetMac(AK, SK) zone := 0 // 您空间(Bucket)所在的区域 c := kodo.New(zone, nil) // 用默认配置创建 Client bucket := c.Bucket("imgdewei") ctx := context.Background() err := bucket.PutFile(ctx, nil, fileName, fileData, nil) if err != nil { fmt.Println(err) return false } else { return true } }
func UploadFile(fileName string, fileSize int, r io.Reader) (ok bool) { ok = false kodo.SetMac(config.QiniuAccessKey, config.QiniuSecretKey) zone := 0 c := kodo.New(zone, nil) bucket := c.Bucket(config.QiniuBucketName) ctx := context.Background() fileSizeIn64 := int64(fileSize) err := bucket.Put(ctx, nil, fileName, r, fileSizeIn64, nil) if err == nil { ok = true } return }
func DeleteFileOnQiNiu(fp, bucket string) error { fn := strings.Replace(fp, QiNiuSpace, "", -1) kodo.SetMac(QiNiuAccessKey, QiNiuSecretKey) zone := 0 // 空间(Bucket)所在的区域 c := kodo.New(zone, nil) // 用默认配置创建 Client b := c.Bucket(bucket) ctx := context.Background() err := b.Delete(ctx, fn) // 删除空间中的某个文件 if err != nil { return err } return nil }
func main() { argsWithoutProg := os.Args[1:] kodo.SetMac("_4TUdWfMQGZ5f2DFFmXbARs7pQLWmiPK-IFbSsw5", "1x0lUvV11qxbWQO1G_XrMm6v-MSsDWJWNCJk2K67") zone := 0 c := kodo.New(zone, nil) bucket := c.Bucket("brpublic") ctx := context.Background() localFile := argsWithoutProg[0] err := bucket.Put(ctx, nil, "rigo/"+localFile, strings.NewReader("what"), int64(len("what")), nil) if err != nil { fmt.Println(err) } else { fmt.Println("upload success") fmt.Println("http://7xj2i2.com2.z0.glb.qiniucdn.com/rigo/" + localFile) } }
func main() { argsWithoutProg := os.Args[1:] kodo.SetMac("_4TUdWfMQGZ5f2DFFmXbARs7pQLWmiPK-IFbSsw5", "1x0lUvV11qxbWQO1G_XrMm6v-MSsDWJWNCJk2K67") zone := 0 c := kodo.New(zone, nil) bucket := c.Bucket("brpublic") ctx := context.Background() from := argsWithoutProg[0] to := argsWithoutProg[1] err := bucket.Move(ctx, "rigo/"+from, "rigo/"+to) //err := bucket.Delete(ctx, "rigo/"+localFile) //err := bucket.PutFile(ctx, nil, "rigo/"+localFile, localFile, nil) if err != nil { fmt.Println(err) } else { fmt.Println("move success") fmt.Println("http://7xj2i2.com2.z0.glb.qiniucdn.com/rigo/" + to) } }
func (q *Qiniu) Do() error { kodo.SetMac(q.AccessKey, q.SecretKey) client := kodo.New(0, nil) bucket := client.Bucket(q.Bucket) log15.Info("Qiniu|Bucket|%s", q.Bucket) err := filepath.Walk(q.Local, func(path string, info os.FileInfo, err error) error { if info.IsDir() { return nil } rel, _ := filepath.Rel(q.Local, path) rel = filepath.ToSlash(rel) ctx := context.Background() var ret interface{} if err = bucket.PutFile(ctx, ret, rel, path, nil); err != nil { return err } log15.Debug("Qiniu|Upload|%s", rel) return nil }) return err }