File tree 7 files changed +57
-32
lines changed
7 files changed +57
-32
lines changed Original file line number Diff line number Diff line change 22
22
from datetime import datetime
23
23
import os
24
24
25
+ from six .moves .urllib .parse import urlsplit
25
26
from google .cloud .storage .constants import _DEFAULT_TIMEOUT
26
27
27
28
@@ -491,3 +492,23 @@ def _raise_if_more_than_one_set(**kwargs):
491
492
)
492
493
493
494
raise ValueError (msg )
495
+
496
+
497
+ def _bucket_bound_hostname_url (host , scheme = None ):
498
+ """Helper to build bucket bound hostname URL.
499
+
500
+ :type host: str
501
+ :param host: Host name.
502
+
503
+ :type scheme: str
504
+ :param scheme: (Optional) Web scheme. If passed, use it
505
+ as a scheme in the result URL.
506
+
507
+ :rtype: str
508
+ :returns: A bucket bound hostname URL.
509
+ """
510
+ url_parts = urlsplit (host )
511
+ if url_parts .scheme and url_parts .netloc :
512
+ return host
513
+
514
+ return "{scheme}://{host}/" .format (scheme = scheme , host = host )
Original file line number Diff line number Diff line change 57
57
from google .cloud .storage ._helpers import _add_generation_match_parameters
58
58
from google .cloud .storage ._helpers import _PropertyMixin
59
59
from google .cloud .storage ._helpers import _scalar_property
60
+ from google .cloud .storage ._helpers import _bucket_bound_hostname_url
60
61
from google .cloud .storage ._helpers import _convert_to_timestamp
61
62
from google .cloud .storage ._helpers import _raise_if_more_than_one_set
62
63
from google .cloud .storage ._signing import generate_signed_url_v2
@@ -516,12 +517,9 @@ def generate_signed_url(
516
517
bucket_name = self .bucket .name
517
518
)
518
519
elif bucket_bound_hostname :
519
- if ":" in bucket_bound_hostname :
520
- api_access_endpoint = bucket_bound_hostname
521
- else :
522
- api_access_endpoint = "{scheme}://{bucket_bound_hostname}" .format (
523
- scheme = scheme , bucket_bound_hostname = bucket_bound_hostname
524
- )
520
+ api_access_endpoint = _bucket_bound_hostname_url (
521
+ bucket_bound_hostname , scheme
522
+ )
525
523
else :
526
524
resource = "/{bucket_name}/{quoted_name}" .format (
527
525
bucket_name = self .bucket .name , quoted_name = quoted_name
Original file line number Diff line number Diff line change 38
38
from google .cloud .storage ._helpers import _validate_name
39
39
from google .cloud .storage ._signing import generate_signed_url_v2
40
40
from google .cloud .storage ._signing import generate_signed_url_v4
41
+ from google .cloud .storage ._helpers import _bucket_bound_hostname_url
41
42
from google .cloud .storage .acl import BucketACL
42
43
from google .cloud .storage .acl import DefaultObjectACL
43
44
from google .cloud .storage .blob import Blob
@@ -2861,12 +2862,9 @@ def generate_signed_url(
2861
2862
bucket_name = self .name
2862
2863
)
2863
2864
elif bucket_bound_hostname :
2864
- if ":" in bucket_bound_hostname :
2865
- api_access_endpoint = bucket_bound_hostname
2866
- else :
2867
- api_access_endpoint = "{scheme}://{bucket_bound_hostname}" .format (
2868
- scheme = scheme , bucket_bound_hostname = bucket_bound_hostname
2869
- )
2865
+ api_access_endpoint = _bucket_bound_hostname_url (
2866
+ bucket_bound_hostname , scheme
2867
+ )
2870
2868
else :
2871
2869
resource = "/{bucket_name}" .format (bucket_name = self .name )
2872
2870
Original file line number Diff line number Diff line change 30
30
from google .cloud .client import ClientWithProject
31
31
from google .cloud .exceptions import NotFound
32
32
from google .cloud .storage ._helpers import _get_storage_host
33
+ from google .cloud .storage ._helpers import _bucket_bound_hostname_url
33
34
from google .cloud .storage ._http import Connection
34
35
from google .cloud .storage ._signing import (
35
36
get_expiration_seconds_v4 ,
@@ -1079,15 +1080,8 @@ def generate_signed_post_policy_v4(
1079
1080
# designate URL
1080
1081
if virtual_hosted_style :
1081
1082
url = "https://{}.storage.googleapis.com/" .format (bucket_name )
1082
-
1083
1083
elif bucket_bound_hostname :
1084
- if ":" in bucket_bound_hostname : # URL includes scheme
1085
- url = bucket_bound_hostname
1086
-
1087
- else : # scheme is given separately
1088
- url = "{scheme}://{host}/" .format (
1089
- scheme = scheme , host = bucket_bound_hostname
1090
- )
1084
+ url = _bucket_bound_hostname_url (bucket_bound_hostname , scheme )
1091
1085
else :
1092
1086
url = "https://storage.googleapis.com/{}/" .format (bucket_name )
1093
1087
Original file line number Diff line number Diff line change @@ -525,6 +525,24 @@ def test_add_generation_match_parameters_tuple(self):
525
525
)
526
526
527
527
528
+ class Test__bucket_bound_hostname_url (unittest .TestCase ):
529
+ def _call_fut (self , ** args ):
530
+ from google .cloud .storage ._helpers import _bucket_bound_hostname_url
531
+
532
+ return _bucket_bound_hostname_url (** args )
533
+
534
+ def test_full_hostname (self ):
535
+ HOST = "scheme://domain.tcl/"
536
+ self .assertEqual (self ._call_fut (host = HOST ), HOST )
537
+
538
+ def test_hostname_and_scheme (self ):
539
+ HOST = "domain.tcl"
540
+ SCHEME = "scheme"
541
+ EXPECTED_URL = SCHEME + "://" + HOST + "/"
542
+
543
+ self .assertEqual (self ._call_fut (host = HOST , scheme = SCHEME ), EXPECTED_URL )
544
+
545
+
528
546
class _Connection (object ):
529
547
def __init__ (self , * responses ):
530
548
self ._responses = responses
Original file line number Diff line number Diff line change @@ -405,6 +405,7 @@ def _generate_signed_url_helper(
405
405
):
406
406
from six .moves .urllib import parse
407
407
from google .cloud ._helpers import UTC
408
+ from google .cloud .storage ._helpers import _bucket_bound_hostname_url
408
409
from google .cloud .storage .blob import _API_ACCESS_ENDPOINT
409
410
from google .cloud .storage .blob import _get_encryption_headers
410
411
@@ -464,12 +465,9 @@ def _generate_signed_url_helper(
464
465
bucket .name
465
466
)
466
467
elif bucket_bound_hostname :
467
- if ":" in bucket_bound_hostname :
468
- expected_api_access_endpoint = bucket_bound_hostname
469
- else :
470
- expected_api_access_endpoint = "{scheme}://{bucket_bound_hostname}" .format (
471
- scheme = scheme , bucket_bound_hostname = bucket_bound_hostname
472
- )
468
+ expected_api_access_endpoint = _bucket_bound_hostname_url (
469
+ bucket_bound_hostname , scheme
470
+ )
473
471
else :
474
472
expected_api_access_endpoint = api_access_endpoint
475
473
expected_resource = "/{}/{}" .format (bucket .name , quoted_name )
Original file line number Diff line number Diff line change @@ -2990,6 +2990,7 @@ def _generate_signed_url_helper(
2990
2990
):
2991
2991
from six .moves .urllib import parse
2992
2992
from google .cloud ._helpers import UTC
2993
+ from google .cloud .storage ._helpers import _bucket_bound_hostname_url
2993
2994
from google .cloud .storage .blob import _API_ACCESS_ENDPOINT
2994
2995
2995
2996
api_access_endpoint = api_access_endpoint or _API_ACCESS_ENDPOINT
@@ -3037,12 +3038,9 @@ def _generate_signed_url_helper(
3037
3038
bucket_name
3038
3039
)
3039
3040
elif bucket_bound_hostname :
3040
- if ":" in bucket_bound_hostname :
3041
- expected_api_access_endpoint = bucket_bound_hostname
3042
- else :
3043
- expected_api_access_endpoint = "{scheme}://{bucket_bound_hostname}" .format (
3044
- scheme = scheme , bucket_bound_hostname = bucket_bound_hostname
3045
- )
3041
+ expected_api_access_endpoint = _bucket_bound_hostname_url (
3042
+ bucket_bound_hostname , scheme
3043
+ )
3046
3044
else :
3047
3045
expected_api_access_endpoint = api_access_endpoint
3048
3046
expected_resource = "/{}" .format (parse .quote (bucket_name ))
You can’t perform that action at this time.
0 commit comments