diff --git a/gcloud/storage/blob.py b/gcloud/storage/blob.py index 2947cef50761..c8ced994f856 100644 --- a/gcloud/storage/blob.py +++ b/gcloud/storage/blob.py @@ -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:: @@ -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 @@ -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 @@ -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) diff --git a/gcloud/storage/test_blob.py b/gcloud/storage/test_blob.py index 2b958e6d4308..868498555587 100644 --- a/gcloud/storage/test_blob.py +++ b/gcloud/storage/test_blob.py @@ -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, @@ -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) @@ -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, @@ -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,