/** * [getClient 连接七牛客户端] * @return *Client [七牛客户端] */ func (this *QiniuSdk) getClient() *kodo.Client { cfg := &kodo.Config{ AccessKey: this.AccessKey, SecretKey: this.SecretKey, } return kodo.New(0, cfg) }
//初始化七牛服务 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 upload(filepath string) { //初始化AK,SK conf.ACCESS_KEY = "JRxl723OpszbaeNZZ9kdvlqcylRF18a8gLSTmOie" conf.SECRET_KEY = "H5IbPfFgJWCdzOTnl1YMuhP1rVycAJgDnGD-3cyl" //创建一个Client c := kodo.New(0, nil) //设置上传的策略 policy := &kodo.PutPolicy{ Scope: bucket, //设置Token过期时间 Expires: 3600, } //生成一个上传token token := c.MakeUptoken(policy) //构建一个uploader zone := 0 uploader := kodocli.NewUploader(zone, nil) var ret PutRet //设置上传文件的路径 // filepath := "/home/xinglong/blog" //调用PutFileWithoutKey方式上传,没有设置saveasKey以文件的hash命名 res := uploader.PutFileWithoutKey(nil, &ret, token, filepath, nil) //打印返回的信息 fmt.Println(ret) //打印出错信息 if res != nil { fmt.Println("io.Put failed:", res) return } }
func New(params DriverParameters) (*Driver, error) { client := kodo.New(params.Zone, ¶ms.Config) bucket := client.Bucket(params.Bucket) params.RootDirectory = strings.TrimRight(params.RootDirectory, "/") if !strings.HasSuffix(params.BaseURL, "/") { params.BaseURL += "/" } d := &driver{ params: params, client: client, bucket: &bucket, } return &Driver{ baseEmbed: baseEmbed{ Base: base.Base{ StorageDriver: d, }, }, }, nil }
func main() { flag.Parse() cfg, err := NewConfig(*FlagCfg) if err != nil { log.Fatal(err) } kcfg := &kodo.Config{AccessKey: cfg.AccessKey, SecretKey: cfg.SecretKey} cli := kodo.New(cfg.Zone, kcfg) bucket := cli.Bucket(*FlagBucket) if *FlagList { // TODO: more list entries, _, err := bucket.List(context.TODO(), "", "", 1000) if err != nil { log.Fatal(err) } pretty.Printf("%# v\n", entries) } if *FlagKey != "" && *FlagUrl != "" { err := bucket.Fetch(context.TODO(), *FlagKey, *FlagUrl) if err != nil { log.Fatal(err) } entry, err := bucket.Stat(context.TODO(), *FlagKey) if err != nil { log.Fatal(err) } pretty.Printf("%# v\n", entry) } }
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 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 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 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 }