Skip to content

Instantly share code, notes, and snippets.

@howeyc howeyc/s3size.go
Created May 24, 2015

Embed
What would you like to do?
Size of s3 bucket.
// Get the size of a public s3 bucket.
//
// Relies on the directory listing being public.
//
// Example usage:
// s3size -bucket <bucketName>
//
package main
import (
"encoding/xml"
"flag"
"fmt"
"log"
"net/http"
"net/url"
)
type Content struct {
Key string
Md5Sum string `xml:"ETag"`
Size int64
}
type Result struct {
Contents []Content
IsTruncated bool
}
func main() {
var bucketName string
flag.StringVar(&bucketName, "bucket", "", "bucket to download")
flag.Parse()
bucketUrl := url.URL{Scheme: "https", Host: bucketName + ".s3.amazonaws.com"}
// Request for list of files
// (Possibly requiring multiple requests if bucket has lots of items)
var result Result
for {
reqUrl := bucketUrl
// If we already recieved a response,
// continue from the end of list already received.
if len(result.Contents) > 0 {
reqValues := reqUrl.Query()
reqValues.Set("marker", result.Contents[len(result.Contents)-1].Key)
reqUrl.RawQuery = reqValues.Encode()
}
res, err := http.Get(reqUrl.String())
if err != nil {
log.Fatal(err)
}
xmlDecode := xml.NewDecoder(res.Body)
xmlDecode.Decode(&result)
res.Body.Close()
// Received last of the list
if !result.IsTruncated {
break
}
}
var sum int64
for _, content := range result.Contents {
sum += content.Size
}
fmt.Println(sum)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.