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 Bucket API requests #3492

Merged
merged 10 commits into from
Jun 13, 2017
Prev Previous commit
Next Next commit
Pass 'user_project' if set for 'Bucket.copy_blobs'.
  • Loading branch information
tseaver committed Jun 13, 2017
commit 8a110426a1fe33a6b2ad029023943181a438b771
14 changes: 13 additions & 1 deletion storage/google/cloud/storage/bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -529,14 +529,26 @@ def copy_blob(self, blob, destination_bucket, new_name=None,
:returns: The new Blob.
"""
client = self._require_client(client)
query_params = {}

if self.user_project is not None:
query_params['userProject'] = self.user_project

if new_name is None:
new_name = blob.name

new_blob = Blob(bucket=destination_bucket, name=new_name)
api_path = blob.path + '/copyTo' + new_blob.path
copy_result = client._connection.api_request(
method='POST', path=api_path, _target_object=new_blob)
method='POST',
path=api_path,
query_params=query_params,
_target_object=new_blob,
)

if not preserve_acl:
new_blob.acl.save(acl={}, client=client)

new_blob._set_properties(copy_result)
return new_blob

Expand Down
10 changes: 8 additions & 2 deletions storage/tests/unit/test_bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,7 @@ class _Blob(object):
DEST, BLOB_NAME)
self.assertEqual(kw['method'], 'POST')
self.assertEqual(kw['path'], COPY_PATH)
self.assertEqual(kw['query_params'], {})

def test_copy_blobs_preserve_acl(self):
from google.cloud.storage.acl import ObjectACL
Expand Down Expand Up @@ -597,22 +598,26 @@ class _Blob(object):
self.assertEqual(len(kw), 2)
self.assertEqual(kw[0]['method'], 'POST')
self.assertEqual(kw[0]['path'], COPY_PATH)
self.assertEqual(kw[0]['query_params'], {})
self.assertEqual(kw[1]['method'], 'PATCH')
self.assertEqual(kw[1]['path'], NEW_BLOB_PATH)
self.assertEqual(kw[1]['query_params'], {'projection': 'full'})

def test_copy_blobs_w_name(self):
def test_copy_blobs_w_name_and_user_project(self):
SOURCE = 'source'
DEST = 'dest'
BLOB_NAME = 'blob-name'
NEW_NAME = 'new_name'
USER_PROJECT = 'user-project-123'

class _Blob(object):
name = BLOB_NAME
path = '/b/%s/o/%s' % (SOURCE, BLOB_NAME)

connection = _Connection({})
client = _Client(connection)
source = self._make_one(client=client, name=SOURCE)
source = self._make_one(
client=client, name=SOURCE, user_project=USER_PROJECT)
dest = self._make_one(client=client, name=DEST)
blob = _Blob()
new_blob = source.copy_blob(blob, dest, NEW_NAME)
Expand All @@ -623,6 +628,7 @@ class _Blob(object):
DEST, NEW_NAME)
self.assertEqual(kw['method'], 'POST')
self.assertEqual(kw['path'], COPY_PATH)
self.assertEqual(kw['query_params'], {'userProject': USER_PROJECT})

def test_rename_blob(self):
BUCKET_NAME = 'BUCKET_NAME'
Expand Down