@@ -262,13 +262,13 @@ const (
262262 SigningSchemeV4
263263)
264264
265- // URLStyle determines the style to use for the signed URL. pathStyle is the
265+ // URLStyle determines the style to use for the signed URL. PathStyle is the
266266// default. All non-default options work with V4 scheme only. See
267267// https://cloud.google.com/storage/docs/request-endpoints for details.
268268type URLStyle interface {
269269 // host should return the host portion of the signed URL, not including
270270 // the scheme (e.g. storage.googleapis.com).
271- host (bucket string ) string
271+ host (hostname , bucket string ) string
272272
273273 // path should return the path portion of the signed URL, which may include
274274 // both the bucket and object name or only the object name depending on the
@@ -284,23 +284,27 @@ type bucketBoundHostname struct {
284284 hostname string
285285}
286286
287- func (s pathStyle ) host (bucket string ) string {
287+ func (s pathStyle ) host (hostname , bucket string ) string {
288+ if hostname != "" {
289+ return stripScheme (hostname )
290+ }
291+
288292 if host := os .Getenv ("STORAGE_EMULATOR_HOST" ); host != "" {
289293 return stripScheme (host )
290294 }
291295
292296 return "storage.googleapis.com"
293297}
294298
295- func (s virtualHostedStyle ) host (bucket string ) string {
299+ func (s virtualHostedStyle ) host (_ , bucket string ) string {
296300 if host := os .Getenv ("STORAGE_EMULATOR_HOST" ); host != "" {
297301 return bucket + "." + stripScheme (host )
298302 }
299303
300304 return bucket + ".storage.googleapis.com"
301305}
302306
303- func (s bucketBoundHostname ) host (bucket string ) string {
307+ func (s bucketBoundHostname ) host (_ , bucket string ) string {
304308 return s .hostname
305309}
306310
@@ -321,7 +325,10 @@ func (s bucketBoundHostname) path(bucket, object string) string {
321325}
322326
323327// PathStyle is the default style, and will generate a URL of the form
324- // "storage.googleapis.com/<bucket-name>/<object-name>".
328+ // "<host-name>/<bucket-name>/<object-name>". By default, <host-name> is
329+ // storage.googleapis.com, but setting an endpoint on the storage Client or
330+ // through STORAGE_EMULATOR_HOST overrides this. Setting Hostname on
331+ // SignedURLOptions or PostPolicyV4Options overrides everything else.
325332func PathStyle () URLStyle {
326333 return pathStyle {}
327334}
@@ -442,6 +449,12 @@ type SignedURLOptions struct {
442449 // Scheme determines the version of URL signing to use. Default is
443450 // SigningSchemeV2.
444451 Scheme SigningScheme
452+
453+ // Hostname sets the host of the signed URL. This field overrides any
454+ // endpoint set on a storage Client or through STORAGE_EMULATOR_HOST.
455+ // Only compatible with PathStyle URLStyle.
456+ // Optional.
457+ Hostname string
445458}
446459
447460func (opts * SignedURLOptions ) clone () * SignedURLOptions {
@@ -458,6 +471,7 @@ func (opts *SignedURLOptions) clone() *SignedURLOptions {
458471 Style : opts .Style ,
459472 Insecure : opts .Insecure ,
460473 Scheme : opts .Scheme ,
474+ Hostname : opts .Hostname ,
461475 }
462476}
463477
@@ -716,7 +730,7 @@ func signedURLV4(bucket, name string, opts *SignedURLOptions, now time.Time) (st
716730 fmt .Fprintf (buf , "%s\n " , escapedQuery )
717731
718732 // Fill in the hostname based on the desired URL style.
719- u .Host = opts .Style .host (bucket )
733+ u .Host = opts .Style .host (opts . Hostname , bucket )
720734
721735 // Fill in the URL scheme.
722736 if opts .Insecure {
@@ -850,7 +864,7 @@ func signedURLV2(bucket, name string, opts *SignedURLOptions) (string, error) {
850864 }
851865 encoded := base64 .StdEncoding .EncodeToString (b )
852866 u .Scheme = "https"
853- u .Host = PathStyle ().host (bucket )
867+ u .Host = PathStyle ().host (opts . Hostname , bucket )
854868 q := u .Query ()
855869 q .Set ("GoogleAccessId" , opts .GoogleAccessID )
856870 q .Set ("Expires" , fmt .Sprintf ("%d" , opts .Expires .Unix ()))
0 commit comments