當前位置: 首頁>>代碼示例>>Golang>>正文


Golang rs.PutPolicy類代碼示例

本文整理匯總了Golang中github.com/qiniu/api/rs.PutPolicy的典型用法代碼示例。如果您正苦於以下問題:Golang PutPolicy類的具體用法?Golang PutPolicy怎麽用?Golang PutPolicy使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


在下文中一共展示了PutPolicy類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: 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)
}
開發者ID:kuguobing,項目名稱:api,代碼行數:7,代碼來源:rsf_api_test.go

示例2: 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
	}
}
開發者ID:HYQMartin,項目名稱:star,代碼行數:34,代碼來源:image_view_test.go

示例3: UploadFile

func UploadFile(dir string, read io.Reader) (string, error) {
	ACCESS_KEY = Webconfig.UploadConfig.QiniuAccessKey
	SECRET_KEY = Webconfig.UploadConfig.QiniuSecretKey

	extra := &qiniu_io.PutExtra{
		//	Bucket:         "messagedream",
		MimeType: "",
	}

	var policy = rs.PutPolicy{
		Scope: "messagedream",
	}

	filename := strings.Replace(UUID(), "-", "", -1) + ".jpg"
	body, _ := ioutil.ReadAll(read)

	h := md5.New()
	h.Write(body)

	key := "static/upload/" + dir + "/" + filename
	ret := new(qiniu_io.PutRet)

	buf := bytes.NewBuffer(body)

	return key, qiniu_io.Put(nil, ret, policy.Token(nil), key, buf, extra)
}
開發者ID:EPICPaaS,項目名稱:MessageBlog,代碼行數:26,代碼來源:upload.go

示例4: handleUpload

func handleUpload(w http.ResponseWriter, req *http.Request) {

	policy := rs.PutPolicy{Scope: BUCKET, ReturnUrl: "http://localhost:8765/uploaded", EndUser: "userId"}
	token := policy.Token(nil)
	log.Println("token:", token)
	uploadForm := fmt.Sprintf(uploadFormFmt, token)
	w.Write([]byte(uploadForm))
}
開發者ID:avivaxrq,項目名稱:form-upload,代碼行數:8,代碼來源:main.go

示例5: handleUploadWithKeyAndCustomField

func handleUploadWithKeyAndCustomField(w http.ResponseWriter, req *http.Request) {

	policy := rs.PutPolicy{Scope: BUCKET, ReturnUrl: "http://localhost:8765/uploaded"}
	token := policy.Token()
	log.Println("token:", token)
	uploadForm := fmt.Sprintf(uploadWithkeyAndCustomFieldFmt, token)
	w.Write([]byte(uploadForm))
}
開發者ID:yunan,項目名稱:Go-Example,代碼行數:8,代碼來源:main.go

示例6: uploadImageHandler

// URL: /upload/image
// 編輯器上傳圖片,接收後上傳到七牛
func uploadImageHandler(handler *Handler) {
	file, header, err := handler.Request.FormFile("editormd-image-file")
	if err != nil {
		panic(err)
		return
	}
	defer file.Close()

	// 檢查是否是jpg或png文件
	uploadFileType := header.Header["Content-Type"][0]

	filenameExtension := ""
	if uploadFileType == "image/jpeg" {
		filenameExtension = ".jpg"
	} else if uploadFileType == "image/png" {
		filenameExtension = ".png"
	} else if uploadFileType == "image/gif" {
		filenameExtension = ".gif"
	}

	if filenameExtension == "" {
		handler.ResponseWriter.Header().Set("Content-Type", "text/html")
		handler.ResponseWriter.Write(editorMdUploadImageResult(0, "", "不支持的文件格式,請上傳 jpg/png/gif 圖片"))
		return
	}

	// 上傳到七牛
	// 文件名:32位uuid+後綴組成
	filename := strings.Replace(uuid.NewUUID().String(), "-", "", -1) + filenameExtension
	key := "upload/image/" + 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 {
		panic(err)

		handler.ResponseWriter.Header().Set("Content-Type", "text/html")
		handler.ResponseWriter.Write(editorMdUploadImageResult(0, "", "圖片上傳到七牛失敗"))

		return
	}

	handler.ResponseWriter.Header().Set("Content-Type", "text/html")
	handler.ResponseWriter.Write(editorMdUploadImageResult(1, "http://7xj1eq.com1.z0.glb.clouddn.com/"+key, ""))
}
開發者ID:makohill,項目名稱:androidfancier.cn,代碼行數:60,代碼來源:views.go

示例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)
	}
}
開發者ID:joozo-gexia,項目名稱:qshell,代碼行數:57,代碼來源:putfile.go

示例8: QiniuTokens

func (this *AdminController) QiniuTokens() {
	bucket := models.Appconf.String("qiniu::bucket")
	putPolicy := rs.PutPolicy{
		Scope: bucket,
	}

	this.Data["json"] = putPolicy.Token(nil)
	this.ServeJson()
}
開發者ID:jango2015,項目名稱:Blog-3,代碼行數:9,代碼來源:admin.go

示例9: qntoken

// generate qiniu token
func qntoken(key string) string {
	scope := defaultBulket + ":" + key
	log.Infof("qiniu scrope: %s", scope)
	policy := rs.PutPolicy{
		Expires: uint32(time.Now().Unix() + 3600),
		Scope:   scope,
	}
	return policy.Token(nil)
}
開發者ID:kissthink,項目名稱:gobuild2,代碼行數:10,代碼來源:xrpc.go

示例10: upFile

func upFile(bucket string, key string, localPath string) error {
	var ret qiniuio.PutRet

	policy := rs.PutPolicy{
		Scope: bucket + ":" + key,
	}
	err := qiniuio.PutFile(nil, &ret, policy.Token(mac), key, localPath, nil)
	log.Printf("ret : %+v", ret)
	return err
}
開發者ID:gavinzhs,項目名稱:testgo,代碼行數:10,代碼來源:qiniu.go

示例11: handleUpload

func handleUpload(w http.ResponseWriter, req *http.Request) {
	policy := rs.PutPolicy{
		Scope:   BUCKET,
		EndUser: "userId",
		SaveKey: "$(sha1)",
	}
	token := policy.Token(nil)
	log.Println("token:", token)
	uploadForm := fmt.Sprintf(uploadFormFmt, token)
	w.Write([]byte(uploadForm))
}
開發者ID:avivaxrq,項目名稱:PiliWebTest,代碼行數:11,代碼來源:gosdk.go

示例12: main

func main() {
	ACCESS_KEY = gopher.Config.QiniuAccessKey
	SECRET_KEY = gopher.Config.QiniuSecretKey

	extra := &qiniu_io.PutExtra{
		Bucket:         "gopher",
		MimeType:       "",
		CustomMeta:     "",
		CallbackParams: "",
	}

	var policy = rs.PutPolicy{
		Scope: "gopher",
	}

	c := gopher.DB.C("users")
	var users []gopher.User
	c.Find(nil).All(&users)

	for _, user := range users {
		url := webhelpers.Gravatar(user.Email, 256)
		resp, err := http.Get(url)
		if err != nil {
			fmt.Println("get gravatar image error:", url, err.Error())
			return
		}

		filename := strings.Replace(uuid.NewUUID().String(), "-", "", -1) + ".jpg"
		body, _ := ioutil.ReadAll(resp.Body)

		h := md5.New()
		h.Write(body)
		md5Str := fmt.Sprintf("%x", h.Sum(nil))

		if md5Str != "ac83818c6d5b6aca4b6f796b6d3cb338" {
			// 不是默認頭像,上傳
			key := "avatar/" + filename
			ret := new(qiniu_io.PutRet)

			buf := bytes.NewBuffer(body)

			err = qiniu_io.Put(nil, ret, policy.Token(), key, buf, extra)
			if err == nil {
				c.Update(bson.M{"_id": user.Id_}, bson.M{"$set": bson.M{"avatar": filename}})
				fmt.Printf("upload %s's avatar success: %s\n", user.Email, filename)
			} else {
				fmt.Printf("upload %s' avatar error: %s\n", user.Email, err.Error())
			}
		}

		resp.Body.Close()
	}
}
開發者ID:hjqhezgh,項目名稱:gopher,代碼行數:53,代碼來源:gravatar2qiniu.go

示例13: GetToken

func GetToken() (string, string) {
	key := uuid.NewV4().String()
	putPolicy := rs.PutPolicy{
		Scope:        g_bucket_name + ":" + key,
		CallbackUrl:  CALLBACK_URL,
		CallbackBody: g_callback_body,
		ReturnUrl:    g_return_url,
		ReturnBody:   g_return_body,
	}

	return putPolicy.Token(nil), key
}
開發者ID:wxaxiaoyao,項目名稱:linux,代碼行數:12,代碼來源:qiniu.go

示例14: qiniuUploadImage

func qiniuUploadImage(file *multipart.File, fileName string) (error, qio.PutRet) {
	var ret qio.PutRet
	var policy = rs.PutPolicy{
		Scope: models.QiniuScope,
	}
	err := qio.Put(nil, &ret, policy.Token(nil), fileName, *file, nil)
	if err != nil {
		revel.ERROR.Println("io.Put failed:", err)
	}

	return err, ret
}
開發者ID:zeuson,項目名稱:gorevel,代碼行數:12,代碼來源:application.go

示例15: uptoken

func uptoken(bucketName string) string {
	putPolicy := rs.PutPolicy{
		Scope: bucketName,
		//CallbackUrl: callbackUrl,
		//CallbackBody:callbackBody,
		//ReturnUrl:   returnUrl,
		//ReturnBody:  returnBody,
		//AsyncOps:    asyncOps,
		//EndUser:     endUser,
		//Expires:     expires,
	}
	return putPolicy.Token(mac)
}
開發者ID:sugeladi,項目名稱:goWeb,代碼行數:13,代碼來源:qiniu.go


注:本文中的github.com/qiniu/api/rs.PutPolicy類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。