Jelajahi Sumber

Support S3 region and enpoint options

DarthSim 6 tahun lalu
induk
melakukan
fc4a09f196
3 mengubah file dengan 20 tambahan dan 3 penghapusan
  1. 5 0
      config.go
  2. 4 2
      docs/serving_files_from_s3.md
  3. 11 1
      s3transport.go

+ 5 - 0
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")

+ 4 - 2
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
 

+ 11 - 1
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) {