Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 22 additions & 14 deletions packages/google-cloud-storage/google/cloud/storage/_signing.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,17 +324,19 @@ def generate_signed_url_v2(
by ``resource``.

:type response_type: str
:param response_type: (Optional) Content type of responses to requests for
the signed URL. Ignored if content_type is set on
object/blob metadata.
:param response_type: (Optional) Only supported with V4 signed URLs
(``version="v4"``). Passing a non-``None`` value
raises :exc:`ValueError`.

:type response_disposition: str
:param response_disposition: (Optional) Content disposition of responses to
requests for the signed URL.
:param response_disposition: (Optional) Only supported with V4 signed URLs
(``version="v4"``). Passing a non-``None``
value raises :exc:`ValueError`.

:type generation: str
:param generation: (Optional) A value that indicates which generation of
the resource to fetch.
:param generation: (Optional) Only supported with V4 signed URLs
(``version="v4"``). Passing a non-``None`` value raises
:exc:`ValueError`.

:type headers: Union[dict|List(Tuple(str,str))]
:param headers:
Expand All @@ -356,6 +358,8 @@ def generate_signed_url_v2(
signed URLs. See:
https://cloud.google.com/storage/docs/xml-api/reference-headers#query

:raises: :exc:`ValueError` when ``response_type``,
``response_disposition``, or ``generation`` is provided.
:raises: :exc:`TypeError` when expiration is not a valid type.
:raises: :exc:`AttributeError` if credentials is not an instance
of :class:`google.auth.credentials.Signing`.
Expand All @@ -364,6 +368,17 @@ def generate_signed_url_v2(
:returns: A signed URL you can use to access the resource
until expiration.
"""
if (
response_type is not None
or response_disposition is not None
or generation is not None
):
raise ValueError(
"The 'response_type', 'response_disposition', and 'generation' "
"parameters are only supported with V4 signed URLs. "
"Use version='v4' instead."
)

expiration_stamp = get_expiration_seconds_v2(expiration)

canonical = canonicalize_v2(method, resource, query_parameters, headers)
Expand Down Expand Up @@ -396,13 +411,6 @@ def generate_signed_url_v2(
credentials, expiration_stamp, string_to_sign
)

if response_type is not None:
signed_query_params["response-content-type"] = response_type
if response_disposition is not None:
signed_query_params["response-content-disposition"] = response_disposition
if generation is not None:
signed_query_params["generation"] = generation

signed_query_params.update(canonical.query_parameters)
sorted_signed_query_params = sorted(signed_query_params.items())

Expand Down
10 changes: 7 additions & 3 deletions packages/google-cloud-storage/google/cloud/storage/blob.py
Original file line number Diff line number Diff line change
Expand Up @@ -524,17 +524,18 @@ def generate_signed_url(
(Optional) Content disposition of responses to requests for the
signed URL. For example, to enable the signed URL to initiate a
file of ``blog.png``, use the value ``'attachment;
filename=blob.png'``.
filename=blob.png'``. Requires ``version="v4"``.

:type response_type: str
:param response_type:
(Optional) Content type of responses to requests for the signed
URL. Ignored if content_type is set on object/blob metadata.
Requires ``version="v4"``.

:type generation: str
:param generation:
(Optional) A value that indicates which generation of the resource
to fetch.
to fetch. Requires ``version="v4"``.

:type headers: dict
:param headers:
Expand Down Expand Up @@ -593,7 +594,10 @@ def generate_signed_url(
hostname, use this value as the scheme. ``https`` will work only
when using a CDN. Defaults to ``"http"``.

:raises: :exc:`ValueError` when version is invalid or mutually exclusive arguments are used.
:raises: :exc:`ValueError` when version is invalid, mutually exclusive
arguments are used, or ``response_type``,
``response_disposition``, or ``generation`` is provided without
``version="v4"``.
:raises: :exc:`ValueError` when ``credentials`` sign the URL, report a
non-default universe domain that differs from the universe
domain of the client, and the URL uses the default endpoint of
Expand Down
38 changes: 15 additions & 23 deletions packages/google-cloud-storage/tests/unit/test__signing.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,9 +337,6 @@ def _generate_helper(
method="GET",
content_md5=None,
content_type=None,
response_type=None,
response_disposition=None,
generation=None,
headers=None,
query_parameters=None,
):
Expand All @@ -361,9 +358,6 @@ def _generate_helper(
method=method,
content_md5=content_md5,
content_type=content_type,
response_type=response_type,
response_disposition=response_disposition,
generation=generation,
headers=headers,
query_parameters=query_parameters,
service_account_email=None,
Expand Down Expand Up @@ -420,17 +414,6 @@ def _generate_helper(
self.assertEqual(params["Expires"], str(expiration))
self.assertEqual(params["Signature"], signed)

if response_type is not None:
self.assertEqual(params["response-content-type"], response_type)

if response_disposition is not None:
self.assertEqual(
params["response-content-disposition"], response_disposition
)

if generation is not None:
self.assertEqual(params["generation"], str(generation))

if query_parameters is not None:
for key, value in query_parameters.items():
value = value.strip() if value else ""
Expand All @@ -451,17 +434,26 @@ def test_w_method_resumable(self):
method = "RESUMABLE"
self._generate_helper(method=method)

def _assert_v4_required(self, **kwargs):
credentials = _make_credentials(signer_email="service@example.com")
credentials.sign_bytes.return_value = b"DEADBEEF"
with self.assertRaisesRegex(ValueError, r"version='v4'"):
self._call_fut(
credentials,
resource="/name/path",
expiration=1000,
**kwargs,
)
credentials.sign_bytes.assert_not_called()

def test_w_response_type(self):
response_type = "text/plain"
self._generate_helper(response_type=response_type)
self._assert_v4_required(response_type="text/plain")

def test_w_response_disposition(self):
response_disposition = "attachment; filename=blob.png"
self._generate_helper(response_disposition=response_disposition)
self._assert_v4_required(response_disposition="attachment; filename=blob.png")

def test_w_generation(self):
generation = "123"
self._generate_helper(generation=generation)
self._assert_v4_required(generation="123")

def test_w_custom_headers_dict(self):
self._generate_helper(headers={"x-goog-foo": "bar"})
Expand Down
Loading