IM development tool-use free object storage services to achieve image upload and download

wedge

When studying the source code of Go Chinese website (https://studygolang.com) studygolang, I found a free-to-use object storage service from its configuration file, which can upload and download pictures, which happened to be provided to my own open source IM project. It's a supplement, it's worth using.

; 图片存储在七牛云,如果没有可以通过 https://portal.qiniu.com/signup?code=3lfz4at7pxfma 免费申请
[qiniu]
access_key = xxxxxx
secret_key = xxxxxxx
bucket_name = xxxx
up_host = xxx
; CND HTTP 的域名
http_domain = xxx
; CND HTTPS 的域名
https_domain = xxx

Next

To apply for a developer account decisively, from application to approval, a total of less than 10 minutes, I have to lament its high efficiency.
welfare

Start of text

No code is written, so test it through postman first.

Create storage space

Enter the space name, select public in the permissions (can be accessed through a URL?), and click Next.

Form upload

First, find the upload URL:

Storage area Regional Abbreviation Upload domain name
East China z0 Server upload: http://up.qiniup.com
Client upload: http://upload.qiniup.com
North China z1 Server upload: http://up-z1.qiniup.com
Client upload: http://upload-z1.qiniup.com
South China z2 Server upload: http://up-z2.qiniup.com
Client upload: http://upload-z2.qiniup.com
Kitami na0 Server upload: http://up-na0.qiniup.com
Client upload: http://upload-na0.qiniup.com
Southeast Asia as0 Server upload: http://up-as0.qiniup.com
Client upload: http://upload-as0.qiniup.com

Because I chose East China, my url is http://upload.qiniup.com , first try it with postman.

Check the official document: https://developer.qiniu.com/kodo/manual/1272/form-upload

<form method="post" action="http://upload.qiniup.com/"
 enctype="multipart/form-data">
  <input name="key" type="hidden" value="<resource_key>">
  <input name="x:<custom_name>" type="hidden" value="<custom_value>">
  <input name="token" type="hidden" value="<upload_token>">
  <input name="crc32" type="hidden" />
  <input name="accept" type="hidden" />
  <input name="file" type="file" />
  <input type="submit" value="上传文件" />
</form>

Only upload_token and file are required.

Get upload_token

The security mechanism seems to be very tightly controlled, and an upload password needs to be generated through code.
Reference: https://github.com/qiniu/api.v7/blob/master/examples/create_uptoken.go

Go code generation (unsuccessful)

Attached an official example (via the generated token, upload the file to report a bad token error):

package main

import (
	"encoding/base64"
	"fmt"
	"os"
	"strings"

	//"github.com/qiniu/api.v7/v7/auth"
	//"github.com/qiniu/api.v7/v7/storage"
    
    // 上面的报错,替换成下面的。执行:go get -u github.com/qiniu/api.v7 安装
    "github.com/qiniu/api.v7/auth"
	"github.com/qiniu/api.v7/storage"
)

var (
	accessKey = os.Getenv("QINIU_ACCESS_KEY") // 在 https://portal.qiniu.com/user/key可以找到
	secretKey = os.Getenv("QINIU_SECRET_KEY")
	bucket    = os.Getenv("QINIU_TEST_BUCKET") // 替换成自己的空间名字
)

func main() {
    
    

	// 简单上传凭证
	putPolicy := storage.PutPolicy{
    
    
		Scope: bucket,
	}
	mac := auth.New(accessKey, secretKey)
	upToken := putPolicy.UploadToken(mac)
	fmt.Println(upToken)

	// 设置上传凭证有效期
	putPolicy = storage.PutPolicy{
    
    
		Scope: bucket,
	}
	putPolicy.Expires = 7200 //示例2小时有效期

	upToken = putPolicy.UploadToken(mac)
	fmt.Println(upToken)

	// 覆盖上传凭证
	// 需要覆盖的文件名
	keyToOverwrite := "qiniu.mp4"
	putPolicy = storage.PutPolicy{
    
    
		Scope: fmt.Sprintf("%s:%s", bucket, keyToOverwrite),
	}
	upToken = putPolicy.UploadToken(mac)
	fmt.Println(upToken)

	// 自定义上传回复凭证
	putPolicy = storage.PutPolicy{
    
    
		Scope:      bucket,
		ReturnBody: `{"key":"$(key)","hash":"$(etag)","fsize":$(fsize),"bucket":"$(bucket)","name":"$(x:name)"}`,
	}
	upToken = putPolicy.UploadToken(mac)
	fmt.Println(upToken)

	// 带回调业务服务器的凭证(JSON方式)
	putPolicy = storage.PutPolicy{
    
    
		Scope:            bucket,
		CallbackURL:      "http://api.example.com/qiniu/upload/callback",
		CallbackBody:     `{"key":"$(key)","hash":"$(etag)","fsize":$(fsize),"bucket":"$(bucket)","name":"$(x:name)"}`,
		CallbackBodyType: "application/json",
	}
	upToken = putPolicy.UploadToken(mac)
	fmt.Println(upToken)

	// 带回调业务服务器的凭证(URL方式)
	putPolicy = storage.PutPolicy{
    
    
		Scope:        bucket,
		CallbackURL:  "http://api.example.com/qiniu/upload/callback",
		CallbackBody: "key=$(key)&hash=$(etag)&bucket=$(bucket)&fsize=$(fsize)&name=$(x:name)",
	}
	upToken = putPolicy.UploadToken(mac)
	fmt.Println(upToken)

	// 带数据处理的凭证
	saveMp4Entry := base64.URLEncoding.EncodeToString([]byte(bucket + ":avthumb_test_target.mp4"))
	saveJpgEntry := base64.URLEncoding.EncodeToString([]byte(bucket + ":vframe_test_target.jpg"))
	//数据处理指令,支持多个指令
	avthumbMp4Fop := "avthumb/mp4|saveas/" + saveMp4Entry
	vframeJpgFop := "vframe/jpg/offset/1|saveas/" + saveJpgEntry
	//连接多个操作指令
	persistentOps := strings.Join([]string{
    
    avthumbMp4Fop, vframeJpgFop}, ";")
	pipeline := "test"
	putPolicy = storage.PutPolicy{
    
    
		Scope:               bucket,
		PersistentOps:       persistentOps,
		PersistentPipeline:  pipeline,
		PersistentNotifyURL: "http://api.example.com/qiniu/pfop/notify",
	}
	upToken = putPolicy.UploadToken(mac)
	fmt.Println(upToken)
}

Dependencies need to be installed before execution

go get -u -v github.com/qiniu/api.v7

Online generation

http://jsfiddle.net/gh/get/extjs/4.2/icattlecoder/jsfiddle/tree/master/uptoken
through the official connection, after opening, replace AK/SK (check here https://portal.qiniu.com /user/key ) and Bucket, click Run in the upper left corner to get the token, put it in postman and upload successfully, but no Bad token error is reported.
Insert picture description here

Test upload using postman

By running the above code, after getting the upload_token, we can use postman to test.
Insert picture description here
One more file.
Insert picture description here

URL format: http:// [domain name] / returned key , such as:
http://q7zje1vvy.bkt.clouddn.com/Fv9V3DRs36_v4clMzCOo-QuSVA_B

The browser opens:
Insert picture description here

reference

Qiniu Cloud upload and download operation guide: https://developer.qiniu.com/kodo/kb/1336/upload-download-instructions

Guess you like

Origin blog.csdn.net/xmcy001122/article/details/105193756