本文整理汇总了Golang中github.com/qiniu/api/v6/rs.PutPolicy类的典型用法代码示例。如果您正苦于以下问题:Golang PutPolicy类的具体用法?Golang PutPolicy怎么用?Golang PutPolicy使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了PutPolicy类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: qiniusave
func qiniusave(file string) (url string, err error) {
var key string
for _, key = range strings.Split(file, "/") {
}
conf.ACCESS_KEY = setting.AccessKeyID
conf.SECRET_KEY = setting.AccessKeysecret
url = "http://" + setting.Endpoint + "/" + key
putPolicy := rs.PutPolicy{Scope: setting.Bucket}
uptoken := putPolicy.Token(nil)
var ret io.PutRet
var extra = &io.PutExtra{}
err = io.PutFile(nil, &ret, uptoken, key, file, extra)
if err != nil {
return "", err
} else {
return url, nil
}
}
示例2: upFile
func upFile(localFile, bucketName, key string) error {
policy := rs.PutPolicy{
Scope: bucketName + ":" + key,
}
return qio.PutFile(nil, nil, policy.Token(nil), key, localFile, nil)
}
示例3: TestPrivateImageView
func TestPrivateImageView(t *testing.T) {
//首先上传一个图片 用于测试
policy := rs.PutPolicy{
Scope: bucket + ":" + key,
}
err := io.PutFile(nil, nil, policy.Token(nil), key, localFile, nil)
if err != nil {
t.Errorf("TestPrivateImageView failed: %v", err)
return
}
rawUrl := makeUrl(key)
iv := ImageView{
Mode: 2,
Height: 250,
Quality: 80,
}
imageViewUrl := iv.MakeRequest(rawUrl)
p := rs.GetPolicy{}
imageViewUrlWithToken := p.MakeRequest(imageViewUrl, nil)
resp, err := http.DefaultClient.Get(imageViewUrlWithToken)
if err != nil {
t.Errorf("TestPrivateImageView failed: %v", err)
return
}
defer resp.Body.Close()
if (resp.StatusCode / 100) != 2 {
t.Errorf("TestPrivateImageView failed: resp.StatusCode = %v", resp.StatusCode)
return
}
}
示例4: token
func (helper *QiniuHelper) token(bucketName string) string {
if helper.cache != nil {
tk, ok := helper.cache.Get(fmt.Sprintf("%s:%s", helper.AccessKey, bucketName))
if ok {
return tk.(string)
}
}
duration := time.Hour
putPolicy := rs.PutPolicy{
Scope: bucketName,
Expires: 3600,
// CallbackUrl: callbackUrl,
// CallbackBody: callbackBody,
// ReturnUrl: returnUrl,
// ReturnBody: returnBody,
// AsyncOps: asyncOps,
// EndUser: endUser,
// Expires: expires,
}
tk2 := putPolicy.Token(nil)
if helper.cache != nil {
helper.cache.Set(fmt.Sprintf("%s:%s", helper.AccessKey, bucketName), tk2, duration)
}
return tk2
}
示例5: genUptoken
func genUptoken(bucket, key string) string {
gr := cfg.Gorelease
if gr.Token != "" {
log.Println("Use gorelease, key:", key)
u := url.URL{
Scheme: "http",
Host: gr.Host,
Path: "/uptoken",
}
query := u.Query()
query.Set("private_token", gr.Token)
query.Set("bucket", bucket)
query.Set("key", key)
u.RawQuery = query.Encode()
resp, err := http.Get(u.String())
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
sysio.Copy(os.Stdout, resp.Body)
log.Fatalf("status: %d", resp.StatusCode)
}
uptoken, _ := ioutil.ReadAll(resp.Body)
return string(uptoken)
}
putPolicy := rs.PutPolicy{
Scope: bucket + ":" + key,
}
return putPolicy.Token(nil)
}
示例6: genUptoken
func genUptoken(bucket string, key string) string {
policy := rs.PutPolicy{
Scope: bucket + ":" + key,
}
policy.Expires = uint32(time.Now().Unix()) + 1800
policy.FsizeLimit = 20 << 20 // 20M
return policy.Token(nil)
}
示例7: ResumablePut
func ResumablePut(cmd string, params ...string) {
if len(params) == 3 || len(params) == 4 || len(params) == 5 {
bucket := params[0]
key := params[1]
localFile := params[2]
mimeType := ""
upHost := "http://upload.qiniu.com"
if len(params) == 4 {
param := params[3]
if strings.HasPrefix(param, "http") {
upHost = param
} else {
mimeType = param
}
}
if len(params) == 5 {
mimeType = params[3]
upHost = params[4]
}
accountS.Get()
mac := digest.Mac{accountS.AccessKey, []byte(accountS.SecretKey)}
policy := rs.PutPolicy{}
policy.Scope = bucket
putExtra := rio.PutExtra{}
if mimeType != "" {
putExtra.MimeType = mimeType
}
conf.UP_HOST = upHost
progressHandler := ProgressHandler{
BlockIndices: make([]int, 0),
BlockProgresses: make(map[int]float32),
}
putExtra.Notify = progressHandler.Notify
putExtra.NotifyErr = progressHandler.NotifyErr
uptoken := policy.Token(&mac)
putRet := rio.PutRet{}
startTime := time.Now()
fStat, statErr := os.Stat(localFile)
if statErr != nil {
log.Error("Local file error", statErr)
return
}
fsize := fStat.Size()
err := rio.PutFile(nil, &putRet, uptoken, key, localFile, &putExtra)
if err != nil {
log.Error("Put file error", err)
} else {
fmt.Println("\r\nPut file", localFile, "=>", bucket, ":", putRet.Key, "(", putRet.Hash, ")", "success!")
}
lastNano := time.Now().UnixNano() - startTime.UnixNano()
lastTime := fmt.Sprintf("%.2f", float32(lastNano)/1e9)
avgSpeed := fmt.Sprintf("%.1f", float32(fsize)*1e6/float32(lastNano))
fmt.Println("Last time:", lastTime, "s, Average Speed:", avgSpeed, "KB/s")
} else {
CmdHelp(cmd)
}
}
示例8: UploadReader
func UploadReader(reader io.Reader, name string) error {
var err error
var ret qio.PutRet
ACCESS_KEY = "NPfcHtb0e2EH7lCmmJot21MRr0lCel81S-QlUaJF"
SECRET_KEY = "6DzF_oVRYhBkq0mqb4txThza_IfQEUey107VXaPq"
var policy = qrs.PutPolicy{
Scope: "isaac",
}
err = qio.Put(nil, &ret, policy.Token(nil), name, reader, nil)
return err
}
示例9: uptoken
func uptoken(bucketName string) string {
putPolicy := qnrs.PutPolicy{
Scope: bucketName,
//CallbackUrl: callbackUrl,
//CallbackBody:callbackBody,
//ReturnUrl: returnUrl,
//ReturnBody: returnBody,
//AsyncOps: asyncOps,
//EndUser: endUser,
//Expires: expires,
}
return putPolicy.Token(nil)
}
示例10: UploadFile
func UploadFile(localFile string, destName string) (addr string, err error) {
policy := new(rs.PutPolicy)
policy.Scope = QiniuScope
uptoken := policy.Token(nil)
var ret io.PutRet
var extra = new(io.PutExtra)
err = io.PutFile(nil, &ret, uptoken, destName, localFile, extra)
if err != nil {
return
}
addr = "http://" + QiniuScope + ".qiniudn.com/" + destName
return
}
示例11: InitQiniu
func InitQiniu() {
conf.ACCESS_KEY = config.Config["qiniu_access_key"]
conf.SECRET_KEY = config.Config["qiniu_secret_key"]
putPolicy := rs.PutPolicy{
Scope: config.Config["qiniu_bucket_name"],
// CallbackUrl: callbackUrl,
// CallbackBody: callbackBody,
// ReturnUrl: returnUrl,
// ReturnBody: returnBody,
// AsyncOps: asyncOps,
// EndUser: endUser,
// Expires: expires,
}
uptoken = putPolicy.Token(nil)
}
示例12: PutFile
func (driver *QiniuDriver) PutFile(key string, data io.Reader, appendData bool) (int64, error) {
var err error
var ret qio.PutRet
var extra = &qio.PutExtra{}
putPolicy := rs.PutPolicy{
Scope: driver.bucket,
}
uptoken := putPolicy.Token(nil)
rd := CountReader(data)
err = qio.Put(nil, &ret, uptoken, strings.TrimLeft(key, "/"), rd, extra)
if err != nil {
return 0, err
}
return int64(rd.Size()), nil
}
示例13: uploadAvatarToQiniu
// 上传到七牛,并返回文件名
func uploadAvatarToQiniu(file io.ReadCloser, contentType string) (filename string, err error) {
isValidateType := false
for _, imgType := range []string{"image/png", "image/jpeg"} {
if imgType == contentType {
isValidateType = true
break
}
}
if !isValidateType {
return "", errors.New("文件类型错误")
}
filenameExtension := ".jpg"
if contentType == "image/png" {
filenameExtension = ".png"
}
// 文件名:32位uuid,不带减号和后缀组成
filename = strings.Replace(uuid.NewUUID().String(), "-", "", -1) + filenameExtension
key := "avatar/" + filename
ret := new(qiniuIo.PutRet)
var policy = rs.PutPolicy{
Scope: "gopher",
}
err = qiniuIo.Put(
nil,
ret,
policy.Token(nil),
key,
file,
nil,
)
if err != nil {
return "", err
}
return filename, nil
}
示例14: TestAll
func TestAll(t *testing.T) {
policy := rs.PutPolicy{
Scope: bucket,
}
token := policy.Token(nil)
params := map[string]string{"x:1": "1"}
extra := &PutExtra{
ChunkSize: 128,
MimeType: "text/plain",
Notify: blockNotify,
Params: params,
}
testPut(t, token, nil)
testPutWithoutKey(t, token, extra)
testPutFile(t, token, extra)
testPutFileWithoutKey(t, token, extra)
testXVar(t, token, extra)
}
示例15: NewUptoken
//内存中new一个uptoken,没有持久化的,有效期为从现在开始的第X天
func (this *Bucket) NewUptoken() error {
if this.Name == "" || this.Ak == "" || this.Sk == "" {
return errors.New("Bucket的Name/Ak/Sk为空,无法生成Uptoken")
}
if this.Life == 0 {
this.Life = 380
}
this.Expires = time.Now().Add(time.Duration(this.Life) * DAY)
putPolicy := rs.PutPolicy{
Scope: this.Name,
Expires: uint32(this.Expires.Unix()),
//CallbackUrl: callbackUrl,
//CallbackBody:callbackBody,
//ReturnUrl: returnUrl,
//ReturnBody: returnBody,
//AsyncOps: asyncOps,
//EndUser: endUser,
}
this.Uptoken = putPolicy.Token(&digest.Mac{this.Ak, []byte(this.Sk)})
this.HasError = false
return nil
}