Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit ac0c769

Browse filesBrowse files
committed
Pass 'user_project' if set for Blob API requests (#3495)
1 parent 8b3456c commit ac0c769
Copy full SHA for ac0c769

File tree

Expand file treeCollapse file tree

2 files changed

+284
-63
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+284
-63
lines changed
Open diff view settings
Collapse file

‎storage/google/cloud/storage/blob.py‎

Copy file name to clipboardExpand all lines: storage/google/cloud/storage/blob.py
+70-13Lines changed: 70 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -338,10 +338,14 @@ def exists(self, client=None):
338338
:returns: True if the blob exists in Cloud Storage.
339339
"""
340340
client = self._require_client(client)
341+
# We only need the status code (200 or not) so we seek to
342+
# minimize the returned payload.
343+
query_params = {'fields': 'name'}
344+
345+
if self.user_project is not None:
346+
query_params['userProject'] = self.user_project
347+
341348
try:
342-
# We only need the status code (200 or not) so we seek to
343-
# minimize the returned payload.
344-
query_params = {'fields': 'name'}
345349
# We intentionally pass `_target_object=None` since fields=name
346350
# would limit the local properties.
347351
client._connection.api_request(
@@ -399,6 +403,8 @@ def _get_download_url(self):
399403
download_url = _DOWNLOAD_URL_TEMPLATE.format(path=self.path)
400404
if self.generation is not None:
401405
download_url += u'&generation={:d}'.format(self.generation)
406+
if self.user_project is not None:
407+
download_url += u'&userProject={}'.format(self.user_project)
402408
return download_url
403409
else:
404410
return self.media_link
@@ -649,6 +655,10 @@ def _do_multipart_upload(self, client, stream, content_type,
649655

650656
upload_url = _MULTIPART_URL_TEMPLATE.format(
651657
bucket_path=self.bucket.path)
658+
659+
if self.user_project is not None:
660+
upload_url += '&userProject={}'.format(self.user_project)
661+
652662
upload = MultipartUpload(upload_url, headers=headers)
653663

654664
if num_retries is not None:
@@ -721,6 +731,10 @@ def _initiate_resumable_upload(self, client, stream, content_type,
721731

722732
upload_url = _RESUMABLE_URL_TEMPLATE.format(
723733
bucket_path=self.bucket.path)
734+
735+
if self.user_project is not None:
736+
upload_url += '&userProject={}'.format(self.user_project)
737+
724738
upload = ResumableUpload(upload_url, chunk_size, headers=headers)
725739

726740
if num_retries is not None:
@@ -1074,9 +1088,16 @@ def get_iam_policy(self, client=None):
10741088
the ``getIamPolicy`` API request.
10751089
"""
10761090
client = self._require_client(client)
1091+
1092+
query_params = {}
1093+
1094+
if self.user_project is not None:
1095+
query_params['userProject'] = self.user_project
1096+
10771097
info = client._connection.api_request(
10781098
method='GET',
10791099
path='%s/iam' % (self.path,),
1100+
query_params=query_params,
10801101
_target_object=None)
10811102
return Policy.from_api_repr(info)
10821103

@@ -1099,11 +1120,18 @@ def set_iam_policy(self, policy, client=None):
10991120
the ``setIamPolicy`` API request.
11001121
"""
11011122
client = self._require_client(client)
1123+
1124+
query_params = {}
1125+
1126+
if self.user_project is not None:
1127+
query_params['userProject'] = self.user_project
1128+
11021129
resource = policy.to_api_repr()
11031130
resource['resourceId'] = self.path
11041131
info = client._connection.api_request(
11051132
method='PUT',
11061133
path='%s/iam' % (self.path,),
1134+
query_params=query_params,
11071135
data=resource,
11081136
_target_object=None)
11091137
return Policy.from_api_repr(info)
@@ -1127,12 +1155,17 @@ def test_iam_permissions(self, permissions, client=None):
11271155
request.
11281156
"""
11291157
client = self._require_client(client)
1130-
query = {'permissions': permissions}
1158+
query_params = {'permissions': permissions}
1159+
1160+
if self.user_project is not None:
1161+
query_params['userProject'] = self.user_project
1162+
11311163
path = '%s/iam/testPermissions' % (self.path,)
11321164
resp = client._connection.api_request(
11331165
method='GET',
11341166
path=path,
1135-
query_params=query)
1167+
query_params=query_params)
1168+
11361169
return resp.get('permissions', [])
11371170

11381171
def make_public(self, client=None):
@@ -1162,13 +1195,22 @@ def compose(self, sources, client=None):
11621195
"""
11631196
if self.content_type is None:
11641197
raise ValueError("Destination 'content_type' not set.")
1198+
11651199
client = self._require_client(client)
1200+
query_params = {}
1201+
1202+
if self.user_project is not None:
1203+
query_params['userProject'] = self.user_project
1204+
11661205
request = {
11671206
'sourceObjects': [{'name': source.name} for source in sources],
11681207
'destination': self._properties.copy(),
11691208
}
11701209
api_response = client._connection.api_request(
1171-
method='POST', path=self.path + '/compose', data=request,
1210+
method='POST',
1211+
path=self.path + '/compose',
1212+
query_params=query_params,
1213+
data=request,
11721214
_target_object=self)
11731215
self._set_properties(api_response)
11741216

@@ -1200,14 +1242,20 @@ def rewrite(self, source, token=None, client=None):
12001242
headers.update(_get_encryption_headers(
12011243
source._encryption_key, source=True))
12021244

1245+
query_params = {}
1246+
12031247
if token:
1204-
query_params = {'rewriteToken': token}
1205-
else:
1206-
query_params = {}
1248+
query_params['rewriteToken'] = token
1249+
1250+
if self.user_project is not None:
1251+
query_params['userProject'] = self.user_project
12071252

12081253
api_response = client._connection.api_request(
1209-
method='POST', path=source.path + '/rewriteTo' + self.path,
1210-
query_params=query_params, data=self._properties, headers=headers,
1254+
method='POST',
1255+
path=source.path + '/rewriteTo' + self.path,
1256+
query_params=query_params,
1257+
data=self._properties,
1258+
headers=headers,
12111259
_target_object=self)
12121260
rewritten = int(api_response['totalBytesRewritten'])
12131261
size = int(api_response['objectSize'])
@@ -1238,13 +1286,22 @@ def update_storage_class(self, new_class, client=None):
12381286
raise ValueError("Invalid storage class: %s" % (new_class,))
12391287

12401288
client = self._require_client(client)
1289+
1290+
query_params = {}
1291+
1292+
if self.user_project is not None:
1293+
query_params['userProject'] = self.user_project
1294+
12411295
headers = _get_encryption_headers(self._encryption_key)
12421296
headers.update(_get_encryption_headers(
12431297
self._encryption_key, source=True))
12441298

12451299
api_response = client._connection.api_request(
1246-
method='POST', path=self.path + '/rewriteTo' + self.path,
1247-
data={'storageClass': new_class}, headers=headers,
1300+
method='POST',
1301+
path=self.path + '/rewriteTo' + self.path,
1302+
query_params=query_params,
1303+
data={'storageClass': new_class},
1304+
headers=headers,
12481305
_target_object=self)
12491306
self._set_properties(api_response['resource'])
12501307

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.