Skip to content

Commit

Permalink
Storage: Re-implement Blob.update_storage_class to support multi-requ…
Browse files Browse the repository at this point in the history
…est operations
  • Loading branch information
Erik Webb committed Dec 3, 2018
1 parent 9708da2 commit 4a7c750
Showing 1 changed file with 10 additions and 12 deletions.
22 changes: 10 additions & 12 deletions storage/google/cloud/storage/blob.py
Original file line number Diff line number Diff line change
Expand Up @@ -1511,8 +1511,10 @@ def rewrite(self, source, token=None, client=None):

return api_response["rewriteToken"], rewritten, size

def update_storage_class(self, new_class, client=None, token=None):
"""Update blob's storage class via a rewrite-in-place.
def update_storage_class(self, new_class, client=None):
"""Update blob's storage class via a rewrite-in-place. This helper will
wait for the rewrite to complete before returning, so it may take some
time for larger files.
See
https://cloud.google.com/storage/docs/per-object-storage-class
Expand All @@ -1523,26 +1525,22 @@ def update_storage_class(self, new_class, client=None, token=None):
:type new_class: str
:param new_class: new storage class for the object
:type token: str
:param token: Optional. Token returned from an earlier, not-completed
call to rewrite the same source blob. If passed,
result will include updated status, total bytes written.
:type client: :class:`~google.cloud.storage.client.Client`
:param client: Optional. The client to use. If not passed, falls back
to the ``client`` stored on the blob's bucket.
:rtype: tuple
:returns: See :meth:`~google.cloud.storage.blob.Blob.rewrite` for details.
"""
if new_class not in self._STORAGE_CLASSES:
raise ValueError("Invalid storage class: %s" % (new_class,))

# Update current blob's storage class prior to rewrite
self._patch_property('storageClass', new_class)

# Use `self` as source blob to rewrite-in-place to the current path
return self.rewrite(self, token, client)
# Execute consecutive rewrite operations until operation is done
token = None
while True:
token, _, _ = self.rewrite(self, token=token)
if token is None:
break

cache_control = _scalar_property("cacheControl")
"""HTTP 'Cache-Control' header for this object.
Expand Down

0 comments on commit 4a7c750

Please sign in to comment.