Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding optional content type argument to generate signed URI method #1447

Merged
merged 4 commits into from Feb 17, 2016
Merged
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
44 changes: 25 additions & 19 deletions gcloud/storage/blob.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,9 @@ def public_url(self):
quoted_name=quote(self.name, safe=''))

def generate_signed_url(self, expiration, method='GET',
client=None, credentials=None,
response_type=None, response_disposition=None,
generation=None):
content_type=None,
generation=None, response_disposition=None,
response_type=None, client=None, credentials=None):
"""Generates a signed URL for this blob.

.. note::
Expand All @@ -179,20 +179,13 @@ def generate_signed_url(self, expiration, method='GET',
:type method: str
:param method: The HTTP verb that will be used when requesting the URL.

:type client: :class:`gcloud.storage.client.Client` or ``NoneType``
:param client: (Optional) The client to use. If not passed, falls back
to the ``client`` stored on the blob's bucket.

:type credentials: :class:`oauth2client.client.OAuth2Credentials` or
:class:`NoneType`
:param credentials: (Optional) The OAuth2 credentials to use to sign
the URL. Defaults to the credentials stored on the
client used.
:type content_type: str
:param content_type: (Optional) The content type of the object
referenced by ``resource``.

:type response_type: str
:param response_type: (Optional) Content type of responses to requests
for the signed URL. Used to over-ride the content
type of the underlying blob/object.
:type generation: str
:param generation: (Optional) A value that indicates which generation
of the resource to fetch.

:type response_disposition: str
:param response_disposition: (Optional) Content disposition of
Expand All @@ -202,9 +195,21 @@ def generate_signed_url(self, expiration, method='GET',
the value
``'attachment; filename=blob.png'``.

:type generation: str
:param generation: (Optional) A value that indicates which generation
of the resource to fetch.
:type response_type: str
:param response_type: (Optional) Content type of responses to requests
for the signed URL. Used to over-ride the content
type of the underlying blob/object.

:type client: :class:`gcloud.storage.client.Client` or ``NoneType``
:param client: (Optional) The client to use. If not passed, falls back
to the ``client`` stored on the blob's bucket.


:type credentials: :class:`oauth2client.client.OAuth2Credentials` or
:class:`NoneType`
:param credentials: (Optional) The OAuth2 credentials to use to sign
the URL. Defaults to the credentials stored on the
client used.

:rtype: str
:returns: A signed URL you can use to access the resource
Expand All @@ -222,6 +227,7 @@ def generate_signed_url(self, expiration, method='GET',
credentials, resource=resource,
api_access_endpoint=_API_ACCESS_ENDPOINT,
expiration=expiration, method=method,
content_type=content_type,
response_type=response_type,
response_disposition=response_disposition,
generation=generation)
Expand Down
37 changes: 37 additions & 0 deletions gcloud/storage/test_blob.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ def _basic_generate_signed_url_helper(self, credentials=None):
'expiration': EXPIRATION,
'method': 'GET',
'resource': PATH,
'content_type': None,
'response_type': None,
'response_disposition': None,
'generation': None,
Expand All @@ -155,6 +156,40 @@ def _basic_generate_signed_url_helper(self, credentials=None):
def test_generate_signed_url_w_default_method(self):
self._basic_generate_signed_url_helper()

def test_generate_signed_url_w_content_type(self):
from gcloud._testing import _Monkey
from gcloud.storage import blob as MUT

BLOB_NAME = 'blob-name'
EXPIRATION = '2014-10-16T20:34:37.000Z'
connection = _Connection()
client = _Client(connection)
bucket = _Bucket(client)
blob = self._makeOne(BLOB_NAME, bucket=bucket)
URI = ('http://example.com/abucket/a-blob-name?Signature=DEADBEEF'
'&Expiration=2014-10-16T20:34:37.000Z')

SIGNER = _Signer()
CONTENT_TYPE = "text/html"
with _Monkey(MUT, generate_signed_url=SIGNER):
signed_url = blob.generate_signed_url(EXPIRATION,
content_type=CONTENT_TYPE)
self.assertEqual(signed_url, URI)

PATH = '/name/%s' % (BLOB_NAME,)
EXPECTED_ARGS = (_Connection.credentials,)
EXPECTED_KWARGS = {
'api_access_endpoint': 'https://storage.googleapis.com',
'expiration': EXPIRATION,
'method': 'GET',
'resource': PATH,
'content_type': CONTENT_TYPE,
'response_type': None,
'response_disposition': None,
'generation': None,
}
self.assertEqual(SIGNER._signed, [(EXPECTED_ARGS, EXPECTED_KWARGS)])

def test_generate_signed_url_w_credentials(self):
credentials = object()
self._basic_generate_signed_url_helper(credentials=credentials)
Expand Down Expand Up @@ -183,6 +218,7 @@ def test_generate_signed_url_w_slash_in_name(self):
'expiration': EXPIRATION,
'method': 'GET',
'resource': '/name/parent%2Fchild',
'content_type': None,
'response_type': None,
'response_disposition': None,
'generation': None,
Expand Down Expand Up @@ -214,6 +250,7 @@ def test_generate_signed_url_w_method_arg(self):
'expiration': EXPIRATION,
'method': 'POST',
'resource': PATH,
'content_type': None,
'response_type': None,
'response_disposition': None,
'generation': None,
Expand Down