From fc4a09f196265b8b0bcd45351b6a35d7ac1edd8c Mon Sep 17 00:00:00 2001 From: DarthSim Date: Tue, 13 Nov 2018 19:23:59 +0600 Subject: [PATCH] Support S3 region and enpoint options --- config.go | 5 +++++ docs/serving_files_from_s3.md | 6 ++++-- s3transport.go | 12 +++++++++++- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/config.go b/config.go index 83ddb0bf..3598150d 100644 --- a/config.go +++ b/config.go @@ -152,6 +152,8 @@ type config struct { LocalFileSystemRoot string S3Enabled bool + S3Region string + S3Endpoint string GCSKey string ETagEnabled bool @@ -246,6 +248,9 @@ func init() { strEnvConfig(&conf.LocalFileSystemRoot, "IMGPROXY_LOCAL_FILESYSTEM_ROOT") boolEnvConfig(&conf.S3Enabled, "IMGPROXY_USE_S3") + strEnvConfig(&conf.S3Region, "IMGPROXY_S3_REGION") + strEnvConfig(&conf.S3Endpoint, "IMGPROXY_S3_ENDPOINT") + strEnvConfig(&conf.GCSKey, "IMGPROXY_GCS_KEY") boolEnvConfig(&conf.ETagEnabled, "IMGPROXY_USE_ETAG") diff --git a/docs/serving_files_from_s3.md b/docs/serving_files_from_s3.md index 7aedb762..a298a79e 100644 --- a/docs/serving_files_from_s3.md +++ b/docs/serving_files_from_s3.md @@ -3,8 +3,10 @@ imgproxy can process images from S3 buckets. To use this feature, do the following: 1. Set `IMGPROXY_USE_S3` environment variable as `true`; -2. [Setup credentials](#setup-credentials) to grant access to your bucket; -3. Use `s3://%bucket_name/%file_key` as the source image URL. +1. Specify AWS region with `IMGPROXY_S3_REGION` or `AWS_REGION`; +3. [Setup credentials](#setup-credentials) to grant access to your bucket; +4. _(optional)_ Specify S3 endpoint with `IMGPROXY_S3_ENDPOINT`; +5. Use `s3://%bucket_name/%file_key` as the source image URL. ### Setup credentials diff --git a/s3transport.go b/s3transport.go index c4079c45..6c4ad2dd 100644 --- a/s3transport.go +++ b/s3transport.go @@ -15,7 +15,17 @@ type s3Transport struct { } func newS3Transport() http.RoundTripper { - return s3Transport{s3.New(session.New())} + s3Conf := aws.NewConfig() + + if len(conf.S3Region) != 0 { + s3Conf.WithRegion(conf.S3Region) + } + + if len(conf.S3Endpoint) != 0 { + s3Conf.WithEndpoint(conf.S3Endpoint) + } + + return s3Transport{s3.New(session.New(), s3Conf)} } func (t s3Transport) RoundTrip(req *http.Request) (resp *http.Response, err error) {