diff --git a/cloud_blobstore/__init__.py b/cloud_blobstore/__init__.py index cb6340c..4f7618b 100644 --- a/cloud_blobstore/__init__.py +++ b/cloud_blobstore/__init__.py @@ -87,6 +87,19 @@ def get_user_metadata( """ raise NotImplementedError() + def get_size( + self, + bucket: str, + object_name: str + ) -> int: + """ + Retrieves the filesize + :param bucket: the bucket the object resides in. + :param object_name: the name of the object for which size is being retrieved. + :return: integer equal to filesize in bytes + """ + raise NotImplementedError() + def copy( self, src_bucket: str, src_object_name: str, diff --git a/cloud_blobstore/gs.py b/cloud_blobstore/gs.py index 1ef2b0e..35a43e1 100644 --- a/cloud_blobstore/gs.py +++ b/cloud_blobstore/gs.py @@ -114,6 +114,24 @@ def get_user_metadata( raise BlobNotFoundError() return response.metadata + def get_size( + self, + bucket: str, + object_name: str + ) -> int: + """ + Retrieves the filesize + :param bucket: the bucket the object resides in. + :param object_name: the name of the object for which size is being retrieved. + :return: integer equal to filesize in bytes + """ + bucket_obj = self._ensure_bucket_loaded(bucket) + response = bucket_obj.get_blob(object_name) + if response is None: + raise BlobNotFoundError() + res = response.size + return res + def copy( self, src_bucket: str, src_object_name: str, diff --git a/cloud_blobstore/s3.py b/cloud_blobstore/s3.py index 31cf3c8..572f4d6 100644 --- a/cloud_blobstore/s3.py +++ b/cloud_blobstore/s3.py @@ -197,6 +197,26 @@ def copy( ), ) + def get_size( + self, + bucket: str, + object_name: str + ) -> int: + """ + Retrieves the filesize + :param bucket: the bucket the object resides in. + :param object_name: the name of the object for which size is being retrieved. + :return: integer equal to filesize in bytes + """ + try: + response = self.get_all_metadata(bucket, object_name) + size = response['ContentLength'] + return size + except botocore.exceptions.ClientError as ex: + if str(ex.response['Error']['Code']) == str(requests.codes.not_found): + raise BlobNotFoundError(ex) + raise BlobStoreUnknownError(ex) + def find_next_missing_parts( self, bucket: str, diff --git a/tests/test_blobstore.py b/tests/test_blobstore.py index 44f092d..3a5b3a3 100644 --- a/tests/test_blobstore.py +++ b/tests/test_blobstore.py @@ -104,6 +104,10 @@ def testGet(self): "test_good_source_data_DOES_NOT_EXIST", ) + def testGetSize(self): + sz = self.handle.get_size(self.test_fixtures_bucket, "test_good_source_data/0") + self.assertEqual(sz, 11358) + def testCopy(self): dst_blob_name = infra.generate_test_key()