Created
December 16, 2016 10:06
-
-
Save jemygraw/2438a1f5162e683130b2fd5901767eec to your computer and use it in GitHub Desktop.
get kodo space usage info
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"golang.org/x/net/context" | |
"qiniupkg.com/api.v7/conf" | |
"qiniupkg.com/api.v7/kodo" | |
) | |
const ( | |
SPACE_API = "http://api.qiniu.com/v6/space" | |
) | |
const ( | |
AccessKey = "" | |
SecretKey = "" | |
) | |
/* | |
beginDate - like 20161216 | |
beginTime - like 000000 | |
endDate - like 20161216 | |
endTime - like 235959 | |
interval - 5min or day | |
bucket - optional, bucket name | |
*/ | |
func getSpaceInfo(beginDate, beginTime, endDate, endTime, interval, bucket string) (spaceInfo map[string][]int64, err error) { | |
c := kodo.New(0, nil) | |
ctx := context.TODO() | |
params := map[string][]string{ | |
"begin": []string{fmt.Sprintf("%s%s", beginDate, beginTime)}, | |
"end": []string{fmt.Sprintf("%s%s", endDate, endTime)}, | |
"g": []string{interval}, | |
} | |
if bucket != "" { | |
params["bucket"] = []string{bucket} | |
} | |
callErr := c.CallWithForm(ctx, &spaceInfo, "GET", SPACE_API, params) | |
if callErr != nil { | |
err = fmt.Errorf("get space info error, %s", callErr) | |
return | |
} | |
return | |
} | |
func main() { | |
conf.ACCESS_KEY = AccessKey | |
conf.SECRET_KEY = SecretKey | |
beginDate := "20161216" | |
beginTime := "000000" | |
endDate := "20161216" | |
endTime := "230000" | |
interval := "5min" | |
bucket := "hikalarm" | |
spaceInfo, gErr := getSpaceInfo(beginDate, beginTime, endDate, endTime, interval, bucket) | |
if gErr != nil { | |
fmt.Println(gErr) | |
return | |
} | |
times := spaceInfo["times"] | |
datas := spaceInfo["datas"] | |
if len(times) == 0 { | |
fmt.Println("get space info error, no data found") | |
return | |
} | |
for i := 0; i < len(times); i++ { | |
fmt.Printf("%d\t%d\n", times[i], datas[i]) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment