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
+8.1k Golang : What fmt.Println() can do and println() cannot do
+23.7k Golang : Get ASCII code from a key press(cross-platform) example
+5.9k Get website traffic ranking with Similar Web or Alexa
+6.5k Unix/Linux : Use netstat to find out IP addresses served by your website server
+14.1k Golang: Pad right or print ending(suffix) zero or spaces in fmt.Printf example
+31.9k Golang : Get local IP and MAC address
+10.3k Golang : Edge detection with Sobel method
+9.9k Golang : Get current, epoch time and display by year, month and day
+10.3k Golang : How to tokenize source code with text/scanner package?
+9.4k Golang : How to check if a string with spaces in between is numeric?
+11.4k Golang : Characters limiter example
+11k Golang : Command line file upload program to server example