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
+15.4k Golang : Save(pipe) HTTP response into a file
+12.5k Golang : Print UTF-8 fonts on image example
+30.6k Golang : How to verify uploaded file is image or allowed file types
+13.9k Golang : Image to ASCII art example
+22.4k Golang : Securing password with salt
+9k Golang : Gaussian blur on image and camera video feed examples
+8.7k Golang : Add text to image and get OpenCV's X, Y co-ordinates example
+5.8k List of Golang XML tutorials
+21.5k Golang : How to force compile or remove object files first before rebuild?
+19.5k Golang : How to count the number of repeated characters in a string?
+18.8k Golang : Find IP address from string
+30k Golang : Record voice(audio) from microphone to .WAV file