The company has a set of cloud storage systems compatible with Amazon S3, written in C + + client is very painful, UMU decided to use go to write one.
First find a reliable open source project, run the following command to install:
Go get Github.com/mitchellh/goamz
It also uses the Github.com/vaughan0/go-ini inside.
Then take a look at the example:
Package Mainimport ( "Github.com/mitchellh/goamz/aws" "GITHUB.COM/MITCHELLH/GOAMZ/S3" " Log" "FMT ") func main () { auth, err: = AWS. Envauth () if err! = Nil { log. Fatal (Err) } Client: = S3. New (Auth, AWS. USEast) resp, err: = client. Listbuckets () if err! = Nil { log. Fatal (Err) } log. Print (FMT. Sprintf ("%T%+v", resp. Buckets[0], resp. Buckets[0])}
There are not many places to change: 1. The authentication method can be changed to AWS. GetAuth, but it's easy to expose AccessKey, Secretkey, so UMU the code posted below is still using AWS. Envauth (). 2, AWS. USEast changed to our own. 3. UMU adds the ability to list all files. See the code:
Package Mainimport ("FMT" "Github.com/mitchellh/goamz/aws" "Github.com/mitchellh/goamz/s3" "Log") func main () {auth, ERR: = AWS. Envauth () if err! = Nil {log. Fatal (err)}var CNC = AWS. region{"CNC",//Name "", //Ec2endpoint "http://s3.bj.xs3cnc.com",//S3endpoint "", //S3bucketendpointfalse, S3locationconstraintfalse,//S3lowercasebucket "", //Sdbendpoint "", //Snsendpoint "", //Sqsendpoint "", ///Iamendpoint "", //Elbendpoint "", //Autoscalingendpoint "", //Rdsendpoint "", // Route53endpoint}client: = S3. New (auth, CNC) resp, err: = client. Listbuckets () if err! = Nil {log. Fatal (Err)}for _, B: = Range resp. Buckets {fmt. Printf ("Bucket Name:%s\n", b.name) BC, ERR: = B.getbucketcontents () if Err = = Nil {for _, Key: = Range *BC {fmt. Printf ("\t%s,%s,%d,%s,%s,%s\n", key.) Key, Key. LastModified, key. Size, key. Storageclass,key. Owner.id, key. Owner.displayname)}}}}
The effect is as follows:
Reference object:
[UMU Study Golang] (4) Simple AWS S3 Client