This is a creation in Article, where the information may have evolved or changed.
The official website to see the API, how to use, for the first time to use the Go language I have a little difficulty, hey, see a demo will be, and super simple
Package Main
Import (
"FMT"
. "Github.com/qiniu/api/conf"
"Github.com/qiniu/api/fop"//This place is a pit, an online API. If downloaded, please change the local address, here is the network address, and the official website is not prompted to refer to the following several
"Github.com/qiniu/api/io"
"Github.com/qiniu/api/rs"
"Log"
)
Func init () {
Access_key = "Create your own account key"
Secret_key = "Create your own account 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 () {
Uploading Local Files
Upload ()
5.1 Getting file information
GetFileInfo ()
6.1.1 Viewing image properties
Imageattr ()
5.2 Deleting files
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)//Note Here I this incoming type RSF. Client, and the SDK writes reference *RSF. Client, in the form of an SDK, the invocation of parameters should be Listall (NIL,&RC, "Needkane", "nee")
Listall (Nil, RC, "Needkane", "nee")
}
6.1.1 View diagram like attributes, there can be no AK andSk
Func imageattr () {
var imageUrl = "Http://needkane.qiniudn.com/kane2.jpg"
II: = FOP. imageinfo{}
Inforet, Err: = II. Call (nil, IMAGEURL)
If err! = Nil {
Error generated
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 Deleting files
Func Delfile () {
Bucket: = "self-established space name"
Key: = "Goupload.jpg"
var rscli = Rs. New (Nil)
ERR: = Rscli.delete (nil, bucket, key)
If err! = Nil {
Error generated
Log. Println ("Rs. Copy failed: ", err)
Return
}
}
5.1 Getting file information
Func GetFileInfo () {
var ret rs. Entry
Bucket: = "Attach"
Key: = "Goupload.jpg"
var rscli = Rs. New (Nil)
ret, err: = Rscli.stat (nil, bucket, key)//If it is not used here: =, instead =, you must declare the variable Ret,err by Var above
If err! = Nil {
Error generated
Log. Println ("Rs. Stat failed: ", err)
Return
}
Handling Return Values
Log. PRINTLN (ret)
}
Uploading Local Files
Func upload () {
Uptoken: = Uptoken ("Self-established space name")
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"
The RET variable is used to access the returned information, see IO for details. Putret
Uptoken the upload password generated for the Business Server
Key is the identity of the file store
LocalFile to local file name
Extra for additional information on uploading files, see IO for details. PutExtra, optional
Err = io. Putfile (Nil, &ret, Uptoken, Key, LocalFile, extra)
If err! = Nil {
Upload generates errors
Log. Print ("Io. Putfile failed: ", err)
Return
}
Upload successful, process return value
Log. Print (ret. Hash, ret. Key)
}
Bulk Query Operations
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)
}
}
Combine multiple batches
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)
}
}
Preview feature
Func Makeviewurl (ImageUrl string) string {
/* var view = Fop. imageview{
mode int//thumbnail
width:1000,//width = 0 indicates no width
height:1200,//height = 0 indicates an unqualified height
QUALITY:100,//quality, 1-100
Format: "GIF",//output format, such as JPG, GIF, PNG, TIF, etc.
}*/
var view = Fop. imageview{}
View. Height = 100
View. Width = 500
View. Quality = 100
Return view. MakeRequest (IMAGEURL)
}
//
Get a list of files in bulk
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 {
Unexpected error
Log. Print ("Listall failed:", err)
}
}
There is a slow-response bug where the API is called, but the number of calls is not displayed on the graphical interface
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.