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

Pass 'user_project' if set for blob downloads w/ 'mediaLink' set #3500

Merged
merged 2 commits into from
Jun 26, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
24 changes: 18 additions & 6 deletions storage/google/cloud/storage/blob.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@
import warnings

import httplib2
from six.moves.urllib.parse import parse_qsl
from six.moves.urllib.parse import quote
from six.moves.urllib.parse import urlencode
from six.moves.urllib.parse import urlsplit
from six.moves.urllib.parse import urlunsplit

import google.auth.transport.requests
from google import resumable_media
Expand Down Expand Up @@ -404,14 +408,22 @@ def _get_download_url(self):
:returns: The download URL for the current blob.
"""
if self.media_link is None:
download_url = _DOWNLOAD_URL_TEMPLATE.format(path=self.path)
base_url = _DOWNLOAD_URL_TEMPLATE.format(path=self.path)
scheme, netloc, path, query, frag = urlsplit(base_url)

This comment was marked as spam.

query = parse_qsl(query)
# Only add `generation' for synthesized URLs: 'mediaLink' from
# server presumably already encodes it.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

if self.generation is not None:
download_url += u'&generation={:d}'.format(self.generation)
if self.user_project is not None:
download_url += u'&userProject={}'.format(self.user_project)
return download_url
query.append(('generation', '{:d}'.format(self.generation)))
else:
return self.media_link
scheme, netloc, path, query, frag = urlsplit(self.media_link)

This comment was marked as spam.

This comment was marked as spam.

query = parse_qsl(query)

if self.user_project is not None:
query.append(('userProject', self.user_project))

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.


query = urlencode(query)
return urlunsplit((scheme, netloc, path, query, frag))

def _do_download(self, transport, file_obj, download_url, headers):
"""Perform a download without any error handling.
Expand Down
15 changes: 14 additions & 1 deletion storage/tests/unit/test_blob.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ def test__make_transport(self, fake_session_factory):

def test__get_download_url_with_media_link(self):
blob_name = 'something.txt'
bucket = mock.Mock(spec=[])
bucket = _Bucket(name='IRRELEVANT')

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

blob = self._make_one(blob_name, bucket=bucket)
media_link = 'http://test.invalid'
# Set the media link on the blob
Expand All @@ -375,6 +375,19 @@ def test__get_download_url_with_media_link(self):
download_url = blob._get_download_url()
self.assertEqual(download_url, media_link)

def test__get_download_url_with_media_link_w_user_project(self):
blob_name = 'something.txt'
user_project = 'user-project-123'
bucket = _Bucket(name='IRRELEVANT', user_project=user_project)
blob = self._make_one(blob_name, bucket=bucket)
media_link = 'http://test.invalid'
# Set the media link on the blob
blob._properties['mediaLink'] = media_link

download_url = blob._get_download_url()
self.assertEqual(
download_url, '{}?userProject={}'.format(media_link, user_project))

def test__get_download_url_on_the_fly(self):
blob_name = 'bzzz-fly.txt'
bucket = _Bucket(name='buhkit')
Expand Down