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
+33.6k Golang : Smarter Error Handling with strings.Contains()
+8.7k Golang : Serving HTTP and Websocket from different ports in a program example
+13k Golang : Read from buffered reader until specific number of bytes
+15k Golang : rune literal not terminated error
+24.8k Golang : Get current file path of a file or executable
+16k Golang : Generate QR codes for Google Authenticator App and fix "Cannot interpret QR code" error
+7.6k Golang : What fmt.Println() can do and println() cannot do
+20.8k Golang : How to get time zone and load different time zone?
+39k Golang : Remove dashes(or any character) from string
+5.6k Golang : Extract unicode string from another unicode string example