Skip to content

Commit

Permalink
Merge pull request #2409 from daspecster/add-storage-preserve-acl
Browse files Browse the repository at this point in the history
Preserve ACL when copying or renaming blob.
  • Loading branch information
daspecster committed Sep 28, 2016
2 parents 94a596e + 04ea4f7 commit a7cb215
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
8 changes: 7 additions & 1 deletion storage/google/cloud/storage/bucket.py
Expand Up @@ -440,7 +440,7 @@ def delete_blobs(self, blobs, on_error=None, client=None):
raise

def copy_blob(self, blob, destination_bucket, new_name=None,
client=None):
client=None, preserve_acl=True):
"""Copy the given blob to the given bucket, optionally with a new name.
:type blob: :class:`google.cloud.storage.blob.Blob`
Expand All @@ -458,6 +458,10 @@ def copy_blob(self, blob, destination_bucket, new_name=None,
:param client: Optional. The client to use. If not passed, falls back
to the ``client`` stored on the current bucket.
:type preserve_acl: bool
:param preserve_acl: Optional. Copies ACL from old blob to new blob.
Default: True.
:rtype: :class:`google.cloud.storage.blob.Blob`
:returns: The new Blob.
"""
Expand All @@ -468,6 +472,8 @@ def copy_blob(self, blob, destination_bucket, new_name=None,
api_path = blob.path + '/copyTo' + new_blob.path
copy_result = client.connection.api_request(
method='POST', path=api_path, _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
32 changes: 32 additions & 0 deletions storage/unit_tests/test_bucket.py
Expand Up @@ -534,6 +534,38 @@ class _Blob(object):
self.assertEqual(kw['method'], 'POST')
self.assertEqual(kw['path'], COPY_PATH)

def test_copy_blobs_preserve_acl(self):
from google.cloud.storage.acl import ObjectACL
SOURCE = 'source'
DEST = 'dest'
BLOB_NAME = 'blob-name'
NEW_NAME = 'new_name'
BLOB_PATH = '/b/%s/o/%s' % (SOURCE, BLOB_NAME)
NEW_BLOB_PATH = '/b/%s/o/%s' % (DEST, NEW_NAME)
COPY_PATH = '/b/%s/o/%s/copyTo/b/%s/o/%s' % (SOURCE, BLOB_NAME,
DEST, NEW_NAME)

class _Blob(object):
name = BLOB_NAME
path = BLOB_PATH

connection = _Connection({}, {})
client = _Client(connection)
source = self._makeOne(client=client, name=SOURCE)
dest = self._makeOne(client=client, name=DEST)
blob = _Blob()
new_blob = source.copy_blob(blob, dest, NEW_NAME, client=client,
preserve_acl=False)
self.assertIs(new_blob.bucket, dest)
self.assertEqual(new_blob.name, NEW_NAME)
self.assertIsInstance(new_blob.acl, ObjectACL)
kw = connection._requested
self.assertEqual(len(kw), 2)
self.assertEqual(kw[0]['method'], 'POST')
self.assertEqual(kw[0]['path'], COPY_PATH)
self.assertEqual(kw[1]['method'], 'PATCH')
self.assertEqual(kw[1]['path'], NEW_BLOB_PATH)

def test_copy_blobs_w_name(self):
SOURCE = 'source'
DEST = 'dest'
Expand Down

0 comments on commit a7cb215

Please sign in to comment.