Skip to content

Commit

Permalink
get_size for cloud stores (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
xbrianh authored and ttung committed Oct 24, 2017
1 parent d3c664a commit 4d99550
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 0 deletions.
13 changes: 13 additions & 0 deletions cloud_blobstore/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
18 changes: 18 additions & 0 deletions cloud_blobstore/gs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
20 changes: 20 additions & 0 deletions cloud_blobstore/s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions tests/test_blobstore.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down

0 comments on commit 4d99550

Please sign in to comment.