Golang : List objects in AWS S3 bucket
For this tutorial, we will learn how to list a content of a AWS-S3 bucket.
The code below uses third-party packages from :
"launchpad.net/goamz/s3"
"launchpad.net/goamz/aws"
so before executing the code below.....please :
>go get launchpad.net/goamz/s3
>go get launchpad.net/goamz/aws
first.
Here you go:
package main
import (
"fmt"
"launchpad.net/goamz/aws"
"launchpad.net/goamz/s3"
"os"
)
func main() {
AWSAuth := aws.Auth{
AccessKey: "", // change this to yours
SecretKey: "",
}
region := aws.USEast
// change this to your AWS region
// click on the bucketname in AWS control panel and click Properties
// the region for your bucket should be under "Static Website Hosting" tab
connection := s3.New(AWSAuth, region)
bucket := connection.Bucket("<bucketname>") // change this your bucket name
// get up to 500 objects. Default is 1000 objects
// see https://godoc.org/launchpad.net/goamz/s3#Bucket.List
// on how to configure the parameter
response, err := bucket.List("", "", "", 500)
if err != nil {
fmt.Println(err)
os.Exit(1)
// NOTE : If you get this error message
// Get : 301 response missing Location header
// this is because you are using the wrong region for the bucket
// and if you want to figure out the bucket location automatically
// see http://docs.aws.amazon.com/AmazonS3/latest/API/RESTBucketGETlocation.html
// I've try it out with http.Get() and just getting the authenticating
// requests part right is already too much
// work for this tutorial.
// See http://docs.aws.amazon.com/AmazonS3/latest/API/sig-v4-authenticating-requests.html
// UPDATE 15th Jan 2015: See http://camlistore.org/pkg/misc/amazon/s3/#Client.BucketLocation
}
// if no error, then proceed to list the contents/objects inside the bucket
for _, objects := range response.Contents {
fmt.Println(objects.Key)
// see http://docs.aws.amazon.com/AmazonS3/latest/API/RESTBucketGET.html
// for other information to display
}
}
IF everything is setup correctly, this program should throw out a list of objects inside the bucket. The list will include objects inside sub-folders as well.
For the next tutorial, we will learn how to upload file to S3.
References :
See also : Golang : Upload and download file to/from AWS S3
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+6.6k Golang : How to solve "too many .rsrc sections" error?
+28.9k Golang : Saving(serializing) and reading file with GOB
+15k Golang : Find location by IP address and display with Google Map
+10.6k Golang : Get UDP client IP address and differentiate clients by port number
+10.3k Golang : Resolve domain name to IP4 and IP6 addresses.
+7.9k Golang : How To Use Panic and Recover
+12.3k Golang : Send data to /dev/null a.k.a blackhole with ioutil.Discard
+14.5k Golang : Find commonalities in two slices or arrays example
+18.3k Golang : Find IP address from string
+16.1k Golang : Find out mime type from bytes in buffer
+6.1k Unix/Linux : Use netstat to find out IP addresses served by your website server
+8.5k Golang : Find duplicate files with filepath.Walk