Skip to content

Commit 313b1fa

Browse files
authored
Pass 'user_project' if set for Blob API requests (#3495)
1 parent 04c07a5 commit 313b1fa

2 files changed

Lines changed: 284 additions & 63 deletions

File tree

storage/google/cloud/storage/blob.py

Lines changed: 70 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -340,10 +340,14 @@ def exists(self, client=None):
340340
:returns: True if the blob exists in Cloud Storage.
341341
"""
342342
client = self._require_client(client)
343+
# We only need the status code (200 or not) so we seek to
344+
# minimize the returned payload.
345+
query_params = {'fields': 'name'}
346+
347+
if self.user_project is not None:
348+
query_params['userProject'] = self.user_project
349+
343350
try:
344-
# We only need the status code (200 or not) so we seek to
345-
# minimize the returned payload.
346-
query_params = {'fields': 'name'}
347351
# We intentionally pass `_target_object=None` since fields=name
348352
# would limit the local properties.
349353
client._connection.api_request(
@@ -403,6 +407,8 @@ def _get_download_url(self):
403407
download_url = _DOWNLOAD_URL_TEMPLATE.format(path=self.path)
404408
if self.generation is not None:
405409
download_url += u'&generation={:d}'.format(self.generation)
410+
if self.user_project is not None:
411+
download_url += u'&userProject={}'.format(self.user_project)
406412
return download_url
407413
else:
408414
return self.media_link
@@ -654,6 +660,10 @@ def _do_multipart_upload(self, client, stream, content_type,
654660

655661
upload_url = _MULTIPART_URL_TEMPLATE.format(
656662
bucket_path=self.bucket.path)
663+
664+
if self.user_project is not None:
665+
upload_url += '&userProject={}'.format(self.user_project)
666+
657667
upload = MultipartUpload(upload_url, headers=headers)
658668

659669
if num_retries is not None:
@@ -726,6 +736,10 @@ def _initiate_resumable_upload(self, client, stream, content_type,
726736

727737
upload_url = _RESUMABLE_URL_TEMPLATE.format(
728738
bucket_path=self.bucket.path)
739+
740+
if self.user_project is not None:
741+
upload_url += '&userProject={}'.format(self.user_project)
742+
729743
upload = ResumableUpload(upload_url, chunk_size, headers=headers)
730744

731745
if num_retries is not None:
@@ -1079,9 +1093,16 @@ def get_iam_policy(self, client=None):
10791093
the ``getIamPolicy`` API request.
10801094
"""
10811095
client = self._require_client(client)
1096+
1097+
query_params = {}
1098+
1099+
if self.user_project is not None:
1100+
query_params['userProject'] = self.user_project
1101+
10821102
info = client._connection.api_request(
10831103
method='GET',
10841104
path='%s/iam' % (self.path,),
1105+
query_params=query_params,
10851106
_target_object=None)
10861107
return Policy.from_api_repr(info)
10871108

@@ -1104,11 +1125,18 @@ def set_iam_policy(self, policy, client=None):
11041125
the ``setIamPolicy`` API request.
11051126
"""
11061127
client = self._require_client(client)
1128+
1129+
query_params = {}
1130+
1131+
if self.user_project is not None:
1132+
query_params['userProject'] = self.user_project
1133+
11071134
resource = policy.to_api_repr()
11081135
resource['resourceId'] = self.path
11091136
info = client._connection.api_request(
11101137
method='PUT',
11111138
path='%s/iam' % (self.path,),
1139+
query_params=query_params,
11121140
data=resource,
11131141
_target_object=None)
11141142
return Policy.from_api_repr(info)
@@ -1132,12 +1160,17 @@ def test_iam_permissions(self, permissions, client=None):
11321160
request.
11331161
"""
11341162
client = self._require_client(client)
1135-
query = {'permissions': permissions}
1163+
query_params = {'permissions': permissions}
1164+
1165+
if self.user_project is not None:
1166+
query_params['userProject'] = self.user_project
1167+
11361168
path = '%s/iam/testPermissions' % (self.path,)
11371169
resp = client._connection.api_request(
11381170
method='GET',
11391171
path=path,
1140-
query_params=query)
1172+
query_params=query_params)
1173+
11411174
return resp.get('permissions', [])
11421175

11431176
def make_public(self, client=None):
@@ -1167,13 +1200,22 @@ def compose(self, sources, client=None):
11671200
"""
11681201
if self.content_type is None:
11691202
raise ValueError("Destination 'content_type' not set.")
1203+
11701204
client = self._require_client(client)
1205+
query_params = {}
1206+
1207+
if self.user_project is not None:
1208+
query_params['userProject'] = self.user_project
1209+
11711210
request = {
11721211
'sourceObjects': [{'name': source.name} for source in sources],
11731212
'destination': self._properties.copy(),
11741213
}
11751214
api_response = client._connection.api_request(
1176-
method='POST', path=self.path + '/compose', data=request,
1215+
method='POST',
1216+
path=self.path + '/compose',
1217+
query_params=query_params,
1218+
data=request,
11771219
_target_object=self)
11781220
self._set_properties(api_response)
11791221

@@ -1205,14 +1247,20 @@ def rewrite(self, source, token=None, client=None):
12051247
headers.update(_get_encryption_headers(
12061248
source._encryption_key, source=True))
12071249

1250+
query_params = {}
1251+
12081252
if token:
1209-
query_params = {'rewriteToken': token}
1210-
else:
1211-
query_params = {}
1253+
query_params['rewriteToken'] = token
1254+
1255+
if self.user_project is not None:
1256+
query_params['userProject'] = self.user_project
12121257

12131258
api_response = client._connection.api_request(
1214-
method='POST', path=source.path + '/rewriteTo' + self.path,
1215-
query_params=query_params, data=self._properties, headers=headers,
1259+
method='POST',
1260+
path=source.path + '/rewriteTo' + self.path,
1261+
query_params=query_params,
1262+
data=self._properties,
1263+
headers=headers,
12161264
_target_object=self)
12171265
rewritten = int(api_response['totalBytesRewritten'])
12181266
size = int(api_response['objectSize'])
@@ -1243,13 +1291,22 @@ def update_storage_class(self, new_class, client=None):
12431291
raise ValueError("Invalid storage class: %s" % (new_class,))
12441292

12451293
client = self._require_client(client)
1294+
1295+
query_params = {}
1296+
1297+
if self.user_project is not None:
1298+
query_params['userProject'] = self.user_project
1299+
12461300
headers = _get_encryption_headers(self._encryption_key)
12471301
headers.update(_get_encryption_headers(
12481302
self._encryption_key, source=True))
12491303

12501304
api_response = client._connection.api_request(
1251-
method='POST', path=self.path + '/rewriteTo' + self.path,
1252-
data={'storageClass': new_class}, headers=headers,
1305+
method='POST',
1306+
path=self.path + '/rewriteTo' + self.path,
1307+
query_params=query_params,
1308+
data={'storageClass': new_class},
1309+
headers=headers,
12531310
_target_object=self)
12541311
self._set_properties(api_response['resource'])
12551312

0 commit comments

Comments
 (0)