@@ -399,6 +399,7 @@ def _generate_signed_url_helper(
399399 encryption_key = None ,
400400 access_token = None ,
401401 service_account_email = None ,
402+ virtual_hosted_style = False ,
402403 ):
403404 from six .moves .urllib import parse
404405 from google .cloud ._helpers import UTC
@@ -442,6 +443,7 @@ def _generate_signed_url_helper(
442443 version = version ,
443444 access_token = access_token ,
444445 service_account_email = service_account_email ,
446+ virtual_hosted_style = virtual_hosted_style ,
445447 )
446448
447449 self .assertEqual (signed_uri , signer .return_value )
@@ -452,7 +454,17 @@ def _generate_signed_url_helper(
452454 expected_creds = credentials
453455
454456 encoded_name = blob_name .encode ("utf-8" )
455- expected_resource = "/name/{}" .format (parse .quote (encoded_name , safe = b"/~" ))
457+ quoted_name = parse .quote (encoded_name , safe = b"/~" )
458+
459+ if virtual_hosted_style :
460+ expected_api_access_endpoint = "https://{}.storage.googleapis.com" .format (
461+ bucket .name
462+ )
463+ expected_resource = "/{}" .format (quoted_name )
464+ else :
465+ expected_api_access_endpoint = api_access_endpoint
466+ expected_resource = "/{}/{}" .format (bucket .name , quoted_name )
467+
456468 if encryption_key is not None :
457469 expected_headers = headers or {}
458470 if effective_version == "v2" :
@@ -465,7 +477,7 @@ def _generate_signed_url_helper(
465477 expected_kwargs = {
466478 "resource" : expected_resource ,
467479 "expiration" : expiration ,
468- "api_access_endpoint" : api_access_endpoint ,
480+ "api_access_endpoint" : expected_api_access_endpoint ,
469481 "method" : method .upper (),
470482 "content_md5" : content_md5 ,
471483 "content_type" : content_type ,
@@ -604,6 +616,9 @@ def test_generate_signed_url_v4_w_csek_and_headers(self):
604616 encryption_key = os .urandom (32 ), headers = {"x-goog-foo" : "bar" }
605617 )
606618
619+ def test_generate_signed_url_v4_w_virtual_hostname (self ):
620+ self ._generate_signed_url_v4_helper (virtual_hosted_style = True )
621+
607622 def test_generate_signed_url_v4_w_credentials (self ):
608623 credentials = object ()
609624 self ._generate_signed_url_v4_helper (credentials = credentials )
0 commit comments