官网看api,如何使用,对于第一次使用go语言的我略显吃力,嘿嘿,看了个demo就会了,而且超简单
package main
import (
"fmt"
. "github.com/qiniu/api/conf"
"github.com/qiniu/api/fop"//这个地方是坑,网上的api。如果下载下来,请改本地地址,这里是网络地址,而且官网没提示要引用下面几个
"github.com/qiniu/api/io"
"github.com/qiniu/api/rs"
"log"
)
func init() {
ACCESS_KEY = "自己创建帐号密钥生成的"
SECRET_KEY = "自己创建帐号密钥生成的"
}
//GET upload access token
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(nil)
}
func main() {
//上传本地文件
upload()
//5.1 获取文件信息
//getFileInfo()
//6.1.1 查看图像属性
//imageAttr()
//5.2 删除文件
//delFile()
//delFile2()
//imageAttr2()
//copyFile("needkane", "need", "needcrystal", "needtoo")
//fmt.Println(downloadUrl("needcrystal.qiniudn.com", "needtoo"))
//getBatchInfo()
//highBatch()
//println(makeViewUrl("http://needkane.qiniudn.com/need"))
var rc = rsf.New(nil)//这里注意我这传入类型rsf.Client,而sdk写的是引用*rsf.Client;若采用sdk的形式,则调用传参数时应该是listAll(nil,&rc,"needkane","nee")
listAll(nil, rc, "needkane", "nee")
}
//6.1.1 查看图像属性,可以没有AK和SK
func imageAttr() {
var imageUrl = "http://needkane.qiniudn.com/kane2.jpg"
ii := fop.ImageInfo{}
infoRet, err := ii.Call(nil, imageUrl)
if err != nil {
// 产生错误
log.Println("fop getImageInfo failed:", err)
return
}
log.Println(infoRet.Height, infoRet.Width, infoRet.ColorModel, infoRet.Format)
}
func makeImageInfoUrl(imageUrl string) string {
ii := fop.ImageInfo{}
return ii.MakeRequest(imageUrl)
}
//5.2 删除文件
func delFile() {
bucket := "自己建立的空间名称"
key := "goupload.jpg"
var rsCli = rs.New(nil)
err := rsCli.Delete(nil, bucket, key)
if err != nil {
// 产生错误
log.Println("rs.Copy failed:", err)
return
}
}
//5.1 获取文件信息
func getFileInfo() {
var ret rs.Entry
bucket := "attach"
key := "goupload.jpg"
var rsCli = rs.New(nil)
ret, err := rsCli.Stat(nil, bucket, key)//如果这里不用:= ,改为=,则必须前面通过var声明变量ret,err
if err != nil {
// 产生错误
log.Println("rs.Stat failed:", err)
return
}
// 处理返回值
log.Println(ret)
}
//上传本地文件
func upload() {
uptoken := uptoken("自己建立的空间名称")
fmt.Printf("uptoken:%s\n", uptoken)
var err error
var ret io.PutRet
var extra = &io.PutExtra{
//Params: params,
//MimeType: mieType,
//Crc32: crc32,
//CheckCrc: CheckCrc,
}
var key = "kane2.jpg"
var localFile = "/root/Desktop/kane.jpg"
// ret 变量用于存取返回的信息,详情见 io.PutRet
// uptoken 为业务服务器生成的上传口令
// key 为文件存储的标识
// localFile 为本地文件名
// extra 为上传文件的额外信息,详情见 io.PutExtra,可选
err = io.PutFile(nil, &ret, uptoken, key, localFile, extra)
if err != nil {
//上传产生错误
log.Print("io.PutFile failed:", err)
return
}
//上传成功,处理返回值
log.Print(ret.Hash, ret.Key)
}
//批量查询操作
func getBatchInfo() {
entryPathes := []rs.EntryPath{
rs.EntryPath{
Bucket: "needkane",
Key: "kane3.jpg",
},
rs.EntryPath{
Bucket: "needcrystal",
Key: "needtoo",
},
}
var batchStatRests []rs.BatchStatItemRet
var rsCli = rs.New(nil)
batchStatRests, err := rsCli.BatchStat(nil, entryPathes)
if err != nil {
log.Println("rs.BatchStat failed", err)
return
}
for time, item := range batchStatRests {
log.Println(time, ";;;;", item)
}
}
//联合多种批处理
func highBatch() {
ops := []string{
rs.URIStat("needkane", "need"),
rs.URICopy("needkane", "kane3.jpg", "needcrystal", "kane"),
rs.URIDelete("needkane", "kane3.jpg"),
}
rets := new([]rs.BatchItemRet)
var rsCli = rs.New(nil)
err := rsCli.Batch(nil, rets, ops)
if err != nil {
log.Println("rs.Batch failed:", err)
return
}
for time, ret := range *rets {
log.Println(time, "::", ret.Code, ret.Error)
}
}
//预览功能
func makeViewUrl(imageUrl string) string {
/* var view = fop.ImageView{
//Mode int // 缩略模式
Width: 1000, // Width = 0 表示不限定宽度
Height: 1200, // Height = 0 表示不限定高度
Quality: 100, // 质量, 1-100
Format: "gif", // 输出格式,如jpg, gif, png, tif等等
}*/
var view = fop.ImageView{}
view.Height = 100
view.Width = 500
view.Quality = 100
return view.MakeRequest(imageUrl)
}
//
批量获取文件列表
func listAll(l rpc.Logger, rc rsf.Client, bucketName string, prefix string) {
var entries []rsf.ListItem
var marker = ""
var err error
var limit = 1000
for err == nil {
entries, marker, err = rc.ListPrefix(nil, bucketName, prefix, marker, limit)
for _, item := range entries {
log.Print("item:", item)
}
}
if err != io.EOF {
//非预期的错误
log.Print("listAll failed:", err)
}
}
有个响应迟钝的bug,这里调用了api,但是图形界面上没有显示调用的次数
版权声明:本文为博主原创文章,未经博主允许不得转载。
有疑问加站长微信联系(非本文作者)