Skip to content

Commit

Permalink
Storage: support 'sourceGeneration' in 'copy_blob' (#4546)
Browse files Browse the repository at this point in the history
Closes #4533.
  • Loading branch information
davidebelloni authored and tseaver committed Dec 21, 2017
1 parent 76a0f5a commit 23512ea
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
9 changes: 8 additions & 1 deletion storage/google/cloud/storage/bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -654,7 +654,7 @@ def delete_blobs(self, blobs, on_error=None, client=None):
raise

def copy_blob(self, blob, destination_bucket, new_name=None,
client=None, preserve_acl=True):
client=None, preserve_acl=True, source_generation=None):
"""Copy the given blob to the given bucket, optionally with a new name.
If :attr:`user_project` is set, bills the API request to that project.
Expand All @@ -678,6 +678,10 @@ def copy_blob(self, blob, destination_bucket, new_name=None,
:param preserve_acl: Optional. Copies ACL from old blob to new blob.
Default: True.
:type source_generation: long
:param source_generation: Optional. The generation of the blob to be
copied.
:rtype: :class:`google.cloud.storage.blob.Blob`
:returns: The new Blob.
"""
Expand All @@ -687,6 +691,9 @@ def copy_blob(self, blob, destination_bucket, new_name=None,
if self.user_project is not None:
query_params['userProject'] = self.user_project

if source_generation is not None:
query_params['sourceGeneration'] = source_generation

if new_name is None:
new_name = blob.name

Expand Down
25 changes: 25 additions & 0 deletions storage/tests/unit/test_bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,31 @@ class _Blob(object):
self.assertEqual(kw['path'], COPY_PATH)
self.assertEqual(kw['query_params'], {})

def test_copy_blobs_source_generation(self):
SOURCE = 'source'
DEST = 'dest'
BLOB_NAME = 'blob-name'
GENERATION = 1512565576797178

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)
dest = self._make_one(client=client, name=DEST)
blob = _Blob()
new_blob = source.copy_blob(blob, dest, source_generation=GENERATION)
self.assertIs(new_blob.bucket, dest)
self.assertEqual(new_blob.name, BLOB_NAME)
kw, = connection._requested
COPY_PATH = '/b/%s/o/%s/copyTo/b/%s/o/%s' % (SOURCE, BLOB_NAME,
DEST, BLOB_NAME)
self.assertEqual(kw['method'], 'POST')
self.assertEqual(kw['path'], COPY_PATH)
self.assertEqual(kw['query_params'], {'sourceGeneration' : GENERATION})

def test_copy_blobs_preserve_acl(self):
from google.cloud.storage.acl import ObjectACL

Expand Down

0 comments on commit 23512ea

Please sign in to comment.