本文整理汇总了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)
}
示例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
}
}
示例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)
}
示例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))
}
示例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))
}
示例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, ""))
}
示例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: QiniuTokens
func (this *AdminController) QiniuTokens() {
bucket := models.Appconf.String("qiniu::bucket")
putPolicy := rs.PutPolicy{
Scope: bucket,
}
this.Data["json"] = putPolicy.Token(nil)
this.ServeJson()
}
示例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)
}
示例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
}
示例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))
}
示例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()
}
}
示例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
}
示例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
}
示例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)
}