From daf830bdb4d04a90af2f2db3bc677c7d9399f761 Mon Sep 17 00:00:00 2001 From: Tres Seaver Date: Tue, 27 Aug 2019 18:32:45 -0400 Subject: [PATCH] Make 'Blob.bucket' a readonly property. (#9113) Closes #9107. --- storage/google/cloud/storage/blob.py | 11 ++++++++++- storage/tests/unit/test_blob.py | 8 ++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/storage/google/cloud/storage/blob.py b/storage/google/cloud/storage/blob.py index dfcaf27a4a7f3..79e6c9cbddb1a 100644 --- a/storage/google/cloud/storage/blob.py +++ b/storage/google/cloud/storage/blob.py @@ -172,7 +172,7 @@ def __init__( super(Blob, self).__init__(name=name) self.chunk_size = chunk_size # Check that setter accepts value. - self.bucket = bucket + self._bucket = bucket self._acl = ObjectACL(self) if encryption_key is not None and kms_key_name is not None: raise ValueError( @@ -187,6 +187,15 @@ def __init__( if generation is not None: self._properties["generation"] = generation + @property + def bucket(self): + """Bucket which contains the object. + + :rtype: :class:`~google.cloud.storage.bucket.Bucket` + :returns: The object's bucket. + """ + return self._bucket + @property def chunk_size(self): """Get the blob's default chunk size. diff --git a/storage/tests/unit/test_blob.py b/storage/tests/unit/test_blob.py index 1a703a78e9b32..e5433da617ac1 100644 --- a/storage/tests/unit/test_blob.py +++ b/storage/tests/unit/test_blob.py @@ -274,6 +274,14 @@ def test_path_with_non_ascii(self): blob = self._make_one(blob_name, bucket=bucket) self.assertEqual(blob.path, "/b/name/o/Caf%C3%A9") + def test_bucket_readonly_property(self): + blob_name = "BLOB" + bucket = _Bucket() + other = _Bucket() + blob = self._make_one(blob_name, bucket=bucket) + with self.assertRaises(AttributeError): + blob.bucket = other + def test_client(self): blob_name = "BLOB" bucket = _Bucket()