Golang : List objects in AWS S3 bucket
Tags : golang AWS s3 objects
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
Tags : golang AWS s3 objects
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
+3.6k Golang : HTTP Routing with Goji example
+4.8k Golang : Read from buffered reader until specific number of bytes
+1.4k Cash Flow : 50 days to pay your credit card debt
446 Golang : Detect words using using consecutive letters in a given string
+1.7k Linux/MacOSX : Search for files by filename and extension with find command
72 Golang : How to call function inside template with template.FuncMap
+15.1k Golang : Display float in 2 decimal points and rounding up or down
+1.7k Javascript : How to loop over and parse JSON data?
+4k Swift : Convert (cast) String to Double
+11.8k Golang : Change file read or write permission example
+1.8k Golang : Create unique title slugs example
+4k Golang : Copy map(hash table) example