Skip to content

Instantly share code, notes, and snippets.

@is
Last active October 15, 2017 11:21
Show Gist options
  • Save is/d4bd5d6cd4d5f695554731079c8954ca to your computer and use it in GitHub Desktop.
Save is/d4bd5d6cd4d5f695554731079c8954ca to your computer and use it in GitHub Desktop.
G27 - OSS Golang demo
package main
import (
"fmt"
"os"
"reflect"
"strconv"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
)
func proxyOption(client *oss.Client) {
client.Config.IsUseProxy = true
client.Config.IsAuthProxy = false
client.Config.ProxyHost = "http://127.0.0.1:11118/"
}
func main() {
host, id, key, bucketName, prefix :=
os.Getenv("OSS_HOST"),
os.Getenv("OSS_ID"),
os.Getenv("OSS_KEY"),
os.Getenv("OSS_BUCKET"),
os.Getenv("PREFIX")
client, err := oss.New(host, id, key, proxyOption)
fmt.Println(client)
buckets, err := client.ListBuckets()
_ = buckets
fmt.Println(err)
bucket, err := client.Bucket(bucketName)
fmt.Println(bucket)
lsRes, err := bucket.ListObjects(oss.Prefix("sp3/"))
for _, object := range lsRes.Objects {
fmt.Println("Objects:", object.Key)
}
body, err := bucket.GetObject("HELLO_WORLD")
if err != nil {
fmt.Println("GetObject", err)
fmt.Println(reflect.TypeOf(err))
err2 := (err.(oss.ServiceError))
fmt.Println(err2.Code)
}
props, err := bucket.GetObjectMeta("sp3/youku/youku.a5ea329.tar.gz")
fmt.Println("Object Meta:", props)
fmt.Println(props["Content-Length"])
fmt.Println(reflect.TypeOf(props["Content-Length"]))
fmt.Println(props["Content-Length"][0])
fmt.Println(strconv.Atoi(props["Content-Length"][0]))
props, err = bucket.GetObjectMeta("HELLO_WORLD")
fmt.Println(err)
_ = props
_ = body
_, _, _, _, _ = host, id, key, bucketName, prefix
_ = err
fmt.Println("Hello world")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment