Example #1
0
File: deploy.go Project: gdb/Stout
func uploadFile(bucket *s3.Bucket, reader io.Reader, dest string, includeHash bool, caching int) string {
	buffer := bytes.NewBuffer([]byte{})

	compress := shouldCompress(dest)

	if compress {
		writer := gzip.NewWriter(buffer)
		must(io.Copy(writer, reader))
		writer.Close()
	} else {
		must(io.Copy(buffer, reader))
	}

	data := buffer.Bytes()

	hash := hashBytes(data)
	hashPrefix := fmt.Sprintf("%x", hash)[:12]
	s3Opts := s3.Options{
		ContentMD5:   base64.StdEncoding.EncodeToString(hash),
		CacheControl: fmt.Sprintf("public, max-age=%d", caching),
	}

	if compress {
		s3Opts.ContentEncoding = "gzip"
	}

	if includeHash {
		dest = filepath.Join(hashPrefix, dest)
	}

	log.Printf("Uploading to %s in %s (%s) [%d]\n", dest, bucket.Name, hashPrefix, caching)
	err := bucket.PutReader(dest, buffer, int64(len(data)), guessContentType(dest), s3.PublicRead, s3Opts)
	panicIf(err)

	return dest
}