Skip to content

Commit a7cb215

Browse files
authored
Merge pull request googleapis#2409 from daspecster/add-storage-preserve-acl
Preserve ACL when copying or renaming blob.
2 parents 94a596e + 04ea4f7 commit a7cb215

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

storage/google/cloud/storage/bucket.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ def delete_blobs(self, blobs, on_error=None, client=None):
440440
raise
441441

442442
def copy_blob(self, blob, destination_bucket, new_name=None,
443-
client=None):
443+
client=None, preserve_acl=True):
444444
"""Copy the given blob to the given bucket, optionally with a new name.
445445
446446
:type blob: :class:`google.cloud.storage.blob.Blob`
@@ -458,6 +458,10 @@ def copy_blob(self, blob, destination_bucket, new_name=None,
458458
:param client: Optional. The client to use. If not passed, falls back
459459
to the ``client`` stored on the current bucket.
460460
461+
:type preserve_acl: bool
462+
:param preserve_acl: Optional. Copies ACL from old blob to new blob.
463+
Default: True.
464+
461465
:rtype: :class:`google.cloud.storage.blob.Blob`
462466
:returns: The new Blob.
463467
"""
@@ -468,6 +472,8 @@ def copy_blob(self, blob, destination_bucket, new_name=None,
468472
api_path = blob.path + '/copyTo' + new_blob.path
469473
copy_result = client.connection.api_request(
470474
method='POST', path=api_path, _target_object=new_blob)
475+
if not preserve_acl:
476+
new_blob.acl.save(acl={}, client=client)
471477
new_blob._set_properties(copy_result)
472478
return new_blob
473479

storage/unit_tests/test_bucket.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,38 @@ class _Blob(object):
534534
self.assertEqual(kw['method'], 'POST')
535535
self.assertEqual(kw['path'], COPY_PATH)
536536

537+
def test_copy_blobs_preserve_acl(self):
538+
from google.cloud.storage.acl import ObjectACL
539+
SOURCE = 'source'
540+
DEST = 'dest'
541+
BLOB_NAME = 'blob-name'
542+
NEW_NAME = 'new_name'
543+
BLOB_PATH = '/b/%s/o/%s' % (SOURCE, BLOB_NAME)
544+
NEW_BLOB_PATH = '/b/%s/o/%s' % (DEST, NEW_NAME)
545+
COPY_PATH = '/b/%s/o/%s/copyTo/b/%s/o/%s' % (SOURCE, BLOB_NAME,
546+
DEST, NEW_NAME)
547+
548+
class _Blob(object):
549+
name = BLOB_NAME
550+
path = BLOB_PATH
551+
552+
connection = _Connection({}, {})
553+
client = _Client(connection)
554+
source = self._makeOne(client=client, name=SOURCE)
555+
dest = self._makeOne(client=client, name=DEST)
556+
blob = _Blob()
557+
new_blob = source.copy_blob(blob, dest, NEW_NAME, client=client,
558+
preserve_acl=False)
559+
self.assertIs(new_blob.bucket, dest)
560+
self.assertEqual(new_blob.name, NEW_NAME)
561+
self.assertIsInstance(new_blob.acl, ObjectACL)
562+
kw = connection._requested
563+
self.assertEqual(len(kw), 2)
564+
self.assertEqual(kw[0]['method'], 'POST')
565+
self.assertEqual(kw[0]['path'], COPY_PATH)
566+
self.assertEqual(kw[1]['method'], 'PATCH')
567+
self.assertEqual(kw[1]['path'], NEW_BLOB_PATH)
568+
537569
def test_copy_blobs_w_name(self):
538570
SOURCE = 'source'
539571
DEST = 'dest'

0 commit comments

Comments
 (0)