From 3d93bc6836b334c2adcba0737847047091aa6f59 Mon Sep 17 00:00:00 2001 From: DarthSim Date: Thu, 7 Apr 2022 23:31:50 +0600 Subject: [PATCH] Add IMGPROXY_GCS_ENDPOINT config --- CHANGELOG.md | 1 + config/config.go | 6 ++++-- docs/configuration.md | 4 +++- ...serving_files_from_google_cloud_storage.md | 3 ++- transport/gcs/gcs.go | 19 ++++++++++++++++--- 5 files changed, 26 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 23e3a318..46ce87d3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ - Add `IMGPROXY_FALLBACK_IMAGE_TTL` config. - (pro) Add [watermark_size](https://docs.imgproxy.net/generating_the_url?id=watermark-size) processing option. - Add OpenStack Object Storage ("Swift") support. +- Add `IMGPROXY_GCS_ENDPOINT` config. ### Change - (pro) Don't check `Content-Length` header of videos. diff --git a/config/config.go b/config/config.go index 881d5fbf..0b210701 100644 --- a/config/config.go +++ b/config/config.go @@ -85,8 +85,9 @@ var ( S3Region string S3Endpoint string - GCSEnabled bool - GCSKey string + GCSEnabled bool + GCSKey string + GCSEndpoint string ABSEnabled bool ABSName string @@ -400,6 +401,7 @@ func Configure() error { configurators.Bool(&GCSEnabled, "IMGPROXY_USE_GCS") configurators.String(&GCSKey, "IMGPROXY_GCS_KEY") + configurators.String(&GCSEndpoint, "IMGPROXY_GCS_ENDPOINT") configurators.Bool(&ABSEnabled, "IMGPROXY_USE_ABS") configurators.String(&ABSName, "IMGPROXY_ABS_NAME") diff --git a/docs/configuration.md b/docs/configuration.md index 6182a106..02f2dd86 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -308,9 +308,11 @@ Check out the [Serving files from S3](serving_files_from_s3.md) guide to learn m ## Serving files from Google Cloud Storage -imgproxy can process files from Google Cloud Storage buckets, but this feature is disabled by default. To enable it, set the value of `IMGPROXY_GCS_KEY` to the content of the Google Cloud JSON key: +imgproxy can process files from Google Cloud Storage buckets, but this feature is disabled by default. To enable it, set the value of `IMGPROXY_USE_GCS` to `true`: +* `IMGPROXY_USE_GCS`: when `true`, enables image fetching from Google Cloud Storage buckets. Default: `false` * `IMGPROXY_GCS_KEY`: the Google Cloud JSON key. When set, enables image fetching from Google Cloud Storage buckets. Default: blank +* `IMGPROXY_GCS_ENDPOINT`: a custom Google Cloud Storage endpoint to being used by imgproxy Check out the [Serving files from Google Cloud Storage](serving_files_from_google_cloud_storage.md) guide to learn more. diff --git a/docs/serving_files_from_google_cloud_storage.md b/docs/serving_files_from_google_cloud_storage.md index c6eaab1c..147c09b8 100644 --- a/docs/serving_files_from_google_cloud_storage.md +++ b/docs/serving_files_from_google_cloud_storage.md @@ -4,7 +4,8 @@ imgproxy can process images from Google Cloud Storage buckets. To use this featu 1. Set the `IMGPROXY_USE_GCS` environment variable to `true`. 2. [Set up credentials](#setup-credentials) to grant access to your bucket. -3. Use `gs://%bucket_name/%file_key` as the source image URL. +3. _(optional)_ Specify the Google Cloud Storage endpoint with `IMGPROXY_GCS_ENDPOINT`. +4. Use `gs://%bucket_name/%file_key` as the source image URL. If you need to specify generation of the source object, you can use the query string of the source URL: diff --git a/transport/gcs/gcs.go b/transport/gcs/gcs.go index c2cedac6..64f490a5 100644 --- a/transport/gcs/gcs.go +++ b/transport/gcs/gcs.go @@ -13,6 +13,9 @@ import ( "github.com/imgproxy/imgproxy/v3/config" ) +// For tests +var noAuth bool = false + type transport struct { client *storage.Client } @@ -23,12 +26,22 @@ func New() (http.RoundTripper, error) { err error ) + opts := []option.ClientOption{} + if len(config.GCSKey) > 0 { - client, err = storage.NewClient(context.Background(), option.WithCredentialsJSON([]byte(config.GCSKey))) - } else { - client, err = storage.NewClient(context.Background()) + opts = append(opts, option.WithCredentialsJSON([]byte(config.GCSKey))) } + if len(config.GCSEndpoint) > 0 { + opts = append(opts, option.WithEndpoint(config.GCSEndpoint)) + } + + if noAuth { + opts = append(opts, option.WithoutAuthentication()) + } + + client, err = storage.NewClient(context.Background(), opts...) + if err != nil { return nil, fmt.Errorf("Can't create GCS client: %s", err) }