Skip to content

Commit ce043bb

Browse files
authored
Making Blob.storage_class settable. (googleapis#3328)
1 parent c9f29c9 commit ce043bb

File tree

2 files changed

+30
-17
lines changed

2 files changed

+30
-17
lines changed

storage/google/cloud/storage/blob.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ class Blob(_PropertyMixin):
9595
9696
.. note::
9797
This list does not include 'DURABLE_REDUCED_AVAILABILITY', which
98-
is only documented for buckets (and deprectated.
98+
is only documented for buckets (and deprecated).
9999
100100
.. note::
101101
The documentation does *not* mention 'STANDARD', but it is the value
@@ -1220,18 +1220,21 @@ def size(self):
12201220
if size is not None:
12211221
return int(size)
12221222

1223-
@property
1224-
def storage_class(self):
1225-
"""Retrieve the storage class for the object.
1223+
storage_class = _scalar_property('storageClass')
1224+
"""Retrieve the storage class for the object.
12261225
1227-
See: https://cloud.google.com/storage/docs/storage-classes
1226+
This can only be set at blob / object **creation** time. If you'd
1227+
like to change the storage class **after** the blob / object already
1228+
exists in a bucket, call :meth:`update_storage_class` (which uses
1229+
the "storage.objects.rewrite" method).
12281230
1229-
:rtype: str or ``NoneType``
1230-
:returns: If set, one of "MULTI_REGIONAL", "REGIONAL",
1231-
"NEARLINE", "COLDLINE", "STANDARD", or
1232-
"DURABLE_REDUCED_AVAILABILITY", else ``None``.
1233-
"""
1234-
return self._properties.get('storageClass')
1231+
See: https://cloud.google.com/storage/docs/storage-classes
1232+
1233+
:rtype: str or ``NoneType``
1234+
:returns: If set, one of "MULTI_REGIONAL", "REGIONAL",
1235+
"NEARLINE", "COLDLINE", "STANDARD", or
1236+
"DURABLE_REDUCED_AVAILABILITY", else ``None``.
1237+
"""
12351238

12361239
@property
12371240
def time_deleted(self):

storage/tests/unit/test_blob.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2057,13 +2057,23 @@ def test_size_string_val(self):
20572057
properties={'size': str(SIZE)})
20582058
self.assertEqual(blob.size, SIZE)
20592059

2060-
def test_storage_class(self):
2061-
BLOB_NAME = 'blob-name'
2060+
def test_storage_class_getter(self):
2061+
blob_name = 'blob-name'
20622062
bucket = _Bucket()
2063-
STORAGE_CLASS = 'http://example.com/self/'
2064-
properties = {'storageClass': STORAGE_CLASS}
2065-
blob = self._make_one(BLOB_NAME, bucket=bucket, properties=properties)
2066-
self.assertEqual(blob.storage_class, STORAGE_CLASS)
2063+
storage_class = 'MULTI_REGIONAL'
2064+
properties = {'storageClass': storage_class}
2065+
blob = self._make_one(blob_name, bucket=bucket, properties=properties)
2066+
self.assertEqual(blob.storage_class, storage_class)
2067+
2068+
def test_storage_class_setter(self):
2069+
blob_name = 'blob-name'
2070+
bucket = _Bucket()
2071+
storage_class = 'COLDLINE'
2072+
blob = self._make_one(blob_name, bucket=bucket)
2073+
self.assertIsNone(blob.storage_class)
2074+
blob.storage_class = storage_class
2075+
self.assertEqual(blob.storage_class, storage_class)
2076+
self.assertEqual(blob._properties, {'storageClass': storage_class})
20672077

20682078
def test_time_deleted(self):
20692079
import datetime

0 commit comments

Comments
 (0)