Skip to content

Commit

Permalink
Change over to using S3
Browse files Browse the repository at this point in the history
  • Loading branch information
pmn committed Apr 7, 2014
1 parent 5c4fa10 commit c1bc60c
Showing 1 changed file with 34 additions and 5 deletions.
39 changes: 34 additions & 5 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ package main
import (
"bytes"
"encoding/gob"
"io/ioutil"
"launchpad.net/goamz/aws"
"launchpad.net/goamz/s3"
"log"
"sort"
"time"
)

// HA HA, joke's on you! ENTIRE DB IS FILE!
const filename = "sparkledb"
const bucketName = "mister-sparkleo"

type SparkleDatabase struct {
Sparkles []Sparkle
Expand All @@ -27,16 +29,43 @@ func (sparkledb *SparkleDatabase) Save() {
panic(err)
}

err = ioutil.WriteFile(filename, data.Bytes(), 0644)
// The AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables are used.
auth, err := aws.EnvAuth()
if err != nil {
panic(err)
panic(err.Error())
}

// Open Bucket
s := s3.New(auth, aws.USEast)

// Load the database from an S3 bucket
bucket := s.Bucket(bucketName)

err = bucket.Put(filename, data.Bytes(), "text/plain", s3.BucketOwnerFull)
if err != nil {
panic(err.Error())
}
}

func LoadDB() SparkleDatabase {
// Load the database from a file
n, err := ioutil.ReadFile(filename)
// The AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables are used.
auth, err := aws.EnvAuth()
if err != nil {
panic(err.Error())
}

// Open Bucket
s := s3.New(auth, aws.USEast)

// Load the database from an S3 bucket
bucket := s.Bucket(bucketName)

// Create a bytes.Buffer
n, err := bucket.Get(filename)
if err != nil {
panic(err)
}

p := bytes.NewBuffer(n)
dec := gob.NewDecoder(p)

Expand Down

0 comments on commit c1bc60c

Please sign in to comment.