Skip to content

Commit

Permalink
Adding sizelimit for file caching
Browse files Browse the repository at this point in the history
  • Loading branch information
Felix Breidenstein committed Nov 9, 2021
1 parent 4e90c72 commit eed5c89
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 12 deletions.
8 changes: 4 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
FROM golang:1.16 AS builder
FROM golang:1.17 AS builder
WORKDIR /src
COPY . .
COPY . .
RUN CGO_ENABLED=0 GOOS=linux go build -o proxy .

FROM alpine:latest
FROM alpine:latest
RUN apk --no-cache add ca-certificates
WORKDIR /
COPY --from=builder /src/proxy .
CMD ["./proxy"]
CMD ["./proxy"]
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,14 @@ docker run -e S3PROXY_BUCKET=nameofmybucket -p 3000:3000 --rm -it codemonauts/s3
## Configuration
All configuration happens via environment variables.

| Name | Required | Default | Description |
| --------------- | :------: | -------------- | ------------------------------------------------------ |
| S3PROXY_BUCKET | x | - | Name of the bucket |
| S3PROXY_REGION | | "eu-central-1" | Region of the bucket |
| S3PROXY_PORT | | "3000" | Listening port of the application |
| S3PROXY_CACHING | | "" | Set this to a path if you wan't the files to be cached |
| S3PROXY_LOGGING | | "WARN" | Loglevel ("ERROR","WARN","INFO","DEBUG") |
| Name | Required | Default | Description |
| ----------------- | :------: | ------------------- | ------------------------------------------------------------ |
| S3PROXY_BUCKET | x | - | Name of the bucket |
| S3PROXY_REGION | | "eu-central-1" | Region of the bucket |
| S3PROXY_PORT | | 3000 | Listening port of the application |
| S3PROXY_CACHING | | "" | Set this to a path if you wan't the files to be cached |
| S3PROXY_SIZELIMIT | | 104857600 ( ~100MB) | Only files smaller than this are cached. Set to 0 to disable |
| S3PROXY_LOGGING | | "WARN" | Loglevel ("ERROR","WARN","INFO","DEBUG") |


## Caching
Expand Down
15 changes: 14 additions & 1 deletion proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/http"
"os"
"path/filepath"
"strconv"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
Expand All @@ -18,6 +19,7 @@ var (
s3Service *s3.S3
bucketName string
cachePath string
cacheLimit int64
)

// getFile checks if we have a local copy otherwise downloads from S3
Expand Down Expand Up @@ -107,6 +109,11 @@ func getFileFromBucket(key string) (FileWrapper, error) {
}

if cachePath != "" {
if *obj.ContentLength > cacheLimit {
log.Infof("Will not cache %q because it's to big (%d byte)\n", key, *obj.ContentLength)
return s3File, nil
}

path, err := saveFileToCache(key, obj)
if err != nil {
// We couldn't save the file to the cache but still return the Get response from S3
Expand Down Expand Up @@ -200,6 +207,7 @@ func main() {
port := envOrDefault("S3PROXY_PORT", "3000")
bucketName = envOrDefault("S3PROXY_BUCKET", "")
cachePath = envOrDefault("S3PROXY_CACHE", "")
cacheLimitEnv := envOrDefault("S3PROXY_SIZELIMIT", "104857600") // Default: 10 MB
logLevel := envOrDefault("S3PROXY_LOGGING", "WARN")

l, err := log.ParseLevel(logLevel)
Expand All @@ -225,13 +233,18 @@ func main() {

}

cacheLimit, err = strconv.ParseInt(cacheLimitEnv, 10, 64)
if err != nil {
log.Fatal("Could not parse the value of S3PROXY_SIZELIMIT into an integer")
}

sess := session.Must(session.NewSession(&aws.Config{
Region: aws.String(region),
}))
s3Service = s3.New(sess)

http.HandleFunc("/", handler)

log.Info("Listening on :%s \n", port)
log.Infof("Listening on :%s \n", port)
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", port), nil))
}

0 comments on commit eed5c89

Please sign in to comment.