Ejemplo n.º 1
0
func main() {
	flag.Parse()

	if len(flag.Args()) < 2 {
		log.Fatal("Missing params: <prefix> and <output>")
	}

	prefix := flag.Arg(0)
	output := flag.Arg(1)

	configJSON, err := os.Open(configFile)
	checkError(err)

	decoder := json.NewDecoder(configJSON)

	c := Configuration{}
	err = decoder.Decode(&c)
	checkError(err)

	s3bucket, err := s3.New(c.Region, c.Bucket)
	checkError(err)
	fmt.Println(s3bucket)
	f, err := os.Create(output)
	if err != nil {
		panic(err)
	}

	err = zipper.Process(s3bucket, f, prefix)
	checkError(err)

}
Ejemplo n.º 2
0
func handler(w http.ResponseWriter, r *http.Request) {
	start := time.Now()

	// Get "id" URL params
	ids, ok := r.URL.Query()["id"]
	if !ok || len(ids) < 1 {
		http.Error(w, "S3 File Zipper. Pass JWT token with ?id=", 400)
		return
	}
	prefix, err := parseJWT(ids[0])
	if err != nil {
		log.Printf("Error decoding JWT: %s\n", err)
		http.Error(w, "Invalid JWT signature", 400)
		return
	}

	zipname := prefix
	if name, ok := r.URL.Query()["name"]; ok && len(name) >= 1 {
		zipname = name[0]
	}

	exists := s3bucket.CacheExists(prefix)

	if exists {
		cache_url, err := s3bucket.CacheSignedUrl(prefix)
		if err != nil {
			http.Error(w, err.Error(), 404)
			return
		}
		//TODO must be converted to Permanent redirection code
		http.Redirect(w, r, cache_url, 302)
		return
	}

	// Start processing the response
	w.Header().Add("Content-Disposition", "attachment; filename=\""+zipname+".zip\"")
	w.Header().Add("Content-Type", "application/zip")

	zipper.Process(s3bucket, w, prefix)

	log.Printf("%s\t%s\t%s", r.Method, r.RequestURI, time.Since(start))
}