[UMU Study Golang] (4) Simple AWS S3 Client

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

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 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:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.