Created
January 9, 2017 13:28
-
-
Save dimitarvp/e4be398504313cc56b5d6c19458bfcf5 to your computer and use it in GitHub Desktop.
A dead-simple Go HTTP streamer/proxy for files hosted in a S3 bucket. NOTE 1: You will need the `AWS_ACCESS_KEY` and `AWS_SECRET_KEY` environment variables set properly in order for this script to work. NOTE 2: This creates a mini HTTP server on localhost:3001.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"github.com/mitchellh/goamz/aws" | |
"github.com/mitchellh/goamz/s3" | |
"log" | |
"net/http" | |
"io" | |
) | |
func checkEnv() aws.Auth { | |
log.Print("Checking for AWS_ACCESS_KEY and AWS_SECRET_KEY environment variables...") | |
auth, err := aws.EnvAuth() | |
if err != nil { | |
panic(err.Error()) | |
} | |
return auth | |
} | |
func streamS3File(path string, bucket *s3.Bucket, w io.Writer) int { | |
log.Printf("Downloading stream at path %#v from bucket %#v...", path, bucket.Name) | |
rc, err := bucket.GetReader(path) | |
if err != nil { | |
panic(err.Error()) | |
} | |
defer rc.Close() | |
buf := make([]byte, 8 * 1048576) | |
n := 0 | |
total := 0 | |
for err == nil { | |
n, err = rc.Read(buf) | |
w.Write(buf[0:n]) | |
total += n | |
} | |
return total | |
} | |
func handler(w http.ResponseWriter, r *http.Request) { | |
path := r.URL.Path | |
streamS3File(path, bucket, w) | |
} | |
func startServing() { | |
http.HandleFunc("/", handler) | |
http.ListenAndServe(":3001", nil) | |
} | |
var auth aws.Auth | |
var client *s3.S3 | |
var bucket *s3.Bucket | |
func main() { | |
auth = checkEnv() | |
client = s3.New(auth, aws.USWest2) | |
bucket = client.Bucket("your-bucket-name") | |
log.Print(bucket.Name) | |
startServing() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment