mirror of
https://github.com/willnorris/imageproxy.git
synced 2025-01-27 23:04:32 -05:00
using s3 as caching store
This commit is contained in:
parent
9213c93c94
commit
4440eaccf4
5 changed files with 33 additions and 9 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1,3 +1,4 @@
|
|||
build
|
||||
.goxc.local.json
|
||||
.idea/*
|
||||
*.test
|
||||
|
|
15
README.md
15
README.md
|
@ -140,13 +140,22 @@ whitelist (meaning any remote URL can be proxied). Test this by navigating to
|
|||
<http://localhost:8080/500/https://octodex.github.com/images/codercat.jpg> and
|
||||
you should see a 500px square coder octocat.
|
||||
|
||||
### Disk cache ###
|
||||
### Caching ###
|
||||
|
||||
By default, the imageproxy command uses an in-memory cache that will grow
|
||||
unbounded. To cache images on disk instead, include the `cacheDir` flag:
|
||||
|
||||
imageproxy -cacheDir /tmp/imageproxy
|
||||
|
||||
Using s3 as a cache, which allows you to add a CDN like CloudFront for resized
|
||||
images, is also possible by using a s3 URL:
|
||||
|
||||
imageproxy -cacheDir s3://s3-us-west-2.amazonaws.com/my-bucket
|
||||
|
||||
S3 cache requires either an IAM role and instance profile with access to your
|
||||
your bucket or `AWS_ACCESS_KEY_ID` and `AWS_SECRET_KEY` environmental parameters
|
||||
set.
|
||||
|
||||
Reload the [codercat URL][], and then inspect the contents of
|
||||
`/tmp/imageproxy`. There should be two files there, one for the original
|
||||
full-size codercat image, and one for the resized 500px version.
|
||||
|
@ -155,7 +164,9 @@ full-size codercat image, and one for the resized 500px version.
|
|||
|
||||
### Referrer Whitelist ###
|
||||
|
||||
You can limit images to only be accessible for certain hosts in the HTTP referrer header. This may be useful to prevent others from hotlinking to images, and using your valuable bandwidth! It can be enabled be running:
|
||||
You can limit images to only be accessible for certain hosts in the HTTP referrer header.
|
||||
This may be useful to prevent others from hotlinking to images, and using your valuable bandwidth!
|
||||
It can be enabled be running:
|
||||
|
||||
imageproxy -referrers example.com
|
||||
|
||||
|
|
BIN
cmd/imageproxy/imageproxy
Executable file
BIN
cmd/imageproxy/imageproxy
Executable file
Binary file not shown.
|
@ -22,11 +22,13 @@ import (
|
|||
"log"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/gregjones/httpcache"
|
||||
"github.com/gregjones/httpcache/diskcache"
|
||||
"github.com/peterbourgon/diskv"
|
||||
"sourcegraph.com/sourcegraph/s3cache"
|
||||
"willnorris.com/go/imageproxy"
|
||||
)
|
||||
|
||||
|
@ -43,7 +45,7 @@ var addr = flag.String("addr", "localhost:8080", "TCP address to listen on")
|
|||
var whitelist = flag.String("whitelist", "", "comma separated list of allowed remote hosts")
|
||||
var referrers = flag.String("referrers", "", "comma separated list of allowed referring hosts")
|
||||
var baseURL = flag.String("baseURL", "", "default base URL for relative remote URLs")
|
||||
var cacheDir = flag.String("cacheDir", "", "directory to use for file cache")
|
||||
var cacheDir = flag.String("cacheDir", "", "directory to use for file cache. can also be s3://s3-us-west-2.amazonaws.com/my-bucket")
|
||||
var cacheSize = flag.Uint64("cacheSize", 100, "maximum size of file cache (in MB)")
|
||||
var signatureKey = flag.String("signatureKey", "", "HMAC key used in calculating request signatures")
|
||||
var scaleUp = flag.Bool("scaleUp", false, "allow images to scale beyond their original dimensions")
|
||||
|
@ -59,11 +61,21 @@ func main() {
|
|||
|
||||
var c httpcache.Cache
|
||||
if *cacheDir != "" {
|
||||
d := diskv.New(diskv.Options{
|
||||
BasePath: *cacheDir,
|
||||
CacheSizeMax: *cacheSize * 1024 * 1024,
|
||||
})
|
||||
c = diskcache.NewWithDiskv(d)
|
||||
_, err := os.Stat(*cacheDir)
|
||||
if err == nil {
|
||||
d := diskv.New(diskv.Options{
|
||||
BasePath: *cacheDir,
|
||||
CacheSizeMax: *cacheSize * 1024 * 1024,
|
||||
})
|
||||
c = diskcache.NewWithDiskv(d)
|
||||
} else if strings.HasPrefix(*cacheDir, "s3://") {
|
||||
fmt.Printf("Using s3 as a cache backend: %v\n", *cacheDir)
|
||||
bucket_url := strings.Replace(*cacheDir, "s3://", "https://", 1)
|
||||
c = s3cache.New(bucket_url)
|
||||
} else {
|
||||
fmt.Printf("Unknown directory or s3 bucket")
|
||||
return
|
||||
}
|
||||
} else if *cacheSize != 0 {
|
||||
c = httpcache.NewMemoryCache()
|
||||
}
|
||||
|
|
|
@ -14,7 +14,7 @@
|
|||
|
||||
// Package imageproxy provides an image proxy server. For typical use of
|
||||
// creating and using a Proxy, see cmd/imageproxy/main.go.
|
||||
package imageproxy // import "willnorris.com/go/imageproxy"
|
||||
package imageproxy
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
|
|
Loading…
Add table
Reference in a new issue