def get_bucket(self, bucket_name): """Returns an object bucket from its name, or None if it does not exist.""" try: request = storage.StorageBucketsGetRequest(bucket=bucket_name) return self.client.buckets.Get(request) except HttpError: return None
def assert_bucket_exists(bucket_name): # type: (str) -> None """Asserts whether the specified GCS bucket with the name bucket_name exists. Logs an error and raises a ValueError if the bucket does not exist. Logs a warning if the bucket cannot be verified to exist. """ try: from apitools.base.py.exceptions import HttpError storage_client = storage.StorageV1( credentials=auth.get_service_credentials(), get_credentials=False, http=get_new_http(), response_encoding='utf8') request = storage.StorageBucketsGetRequest(bucket=bucket_name) storage_client.buckets.Get(request) except HttpError as e: if e.status_code == 404: _LOGGER.error('%s bucket does not exist!', bucket_name) raise ValueError('Invalid GCS bucket provided!') else: _LOGGER.warning( 'HttpError - unable to verify whether bucket %s exists', bucket_name) except ImportError: _LOGGER.warning( 'ImportError - unable to verify whether bucket %s exists', bucket_name)