def _check_creds(self): """ Simple operation to verify creds work without state change :return: True on success """ try: resp = self.client.stat() if resp["success"]: return True elif resp.get("error") and resp.get("error").http_status in [401, 403]: raise BadCredentialsError( self.auth_options, endpoint=None, cause=resp.get("error") ) elif resp.get("error"): raise DriverConfigurationError(cause=resp.get("error")) else: raise DriverConfigurationError( Exception( "Got unsuccessful response from stat operation against service: {}".format( resp ) ) ) except SwiftError as e: raise DriverConfigurationError(e)
def __init__(self, config): super(S3ObjectStorageDriver, self).__init__(config) self.endpoint = None self.region = None self.s3_client = None self.session = None # Initialize the client # if 'access_key' not in self.config: # raise DriverConfigurationError('Missing access_key in configuration for S3 driver') # if 'secret_key' not in self.config: # raise DriverConfigurationError('Missing secret_key in configuration for S3 driver') if 'access_key' in self.config and 'secret_key' in self.config: self.session = boto3.Session( aws_access_key_id=self.config.get('access_key'), aws_secret_access_key=self.config.get('secret_key')) elif self.config.get('iamauto'): self.session = boto3.Session() else: raise DriverConfigurationError( 'Missing either "access_key" and "secret_key" configuration values or "iamauto"=True in configuration for credentials' ) if 'url' in self.config: self.endpoint = self.config.get('url') if not self.endpoint: raise DriverConfigurationError( 'Missing valid value for configuration parameter "url" ({})' .format(self.endpoint)) self.s3_client = self.session.client( service_name='s3', endpoint_url=self.config.get('url')) elif 'region' in self.config: self.region = self.config.get('region') if not self.region: raise DriverConfigurationError( 'Missing valid value for configuration parameter "region" ({})' .format(self.region)) self.s3_client = self.session.client( service_name='s3', region_name=self.config.get('region')) else: self.s3_client = self.session.client(service_name='s3') self.bucket_name = self.config.get('bucket') self.create_bucket = self.config.get('create_bucket', False) if not self.bucket_name: raise ValueError( 'Cannot configure s3 driver with out a provided bucket to use') try: self._check_creds() except BucketNotFoundError: pass self._check_bucket() self.prefix = self.config.get('prefix', '')
def __init__(self, config): super(S3ObjectStorageDriver, self).__init__(config) self.endpoint = None self.region = None self.s3_client = None self.session = None # Initialize the client # if 'access_key' not in self.config: # raise DriverConfigurationError('Missing access_key in configuration for S3 driver') # if 'secret_key' not in self.config: # raise DriverConfigurationError('Missing secret_key in configuration for S3 driver') if "access_key" in self.config and "secret_key" in self.config: self.session = boto3.Session( aws_access_key_id=self.config.get("access_key"), aws_secret_access_key=self.config.get("secret_key"), ) elif self.config.get("iamauto"): self.session = boto3.Session() else: raise DriverConfigurationError( 'Missing either "access_key" and "secret_key" configuration values or "iamauto"=True in configuration for credentials' ) if "url" in self.config: self.endpoint = self.config.get("url") if not self.endpoint: raise DriverConfigurationError( 'Missing valid value for configuration parameter "url" ({})' .format(self.endpoint)) self.s3_client = self.session.client( service_name="s3", endpoint_url=self.config.get("url")) elif "region" in self.config: self.region = self.config.get("region") if not self.region: raise DriverConfigurationError( 'Missing valid value for configuration parameter "region" ({})' .format(self.region)) self.s3_client = self.session.client( service_name="s3", region_name=self.config.get("region")) else: self.s3_client = self.session.client(service_name="s3") self.bucket_name = self.config.get("bucket") self.create_bucket = self.config.get("create_bucket", False) if not self.bucket_name: raise ValueError( "Cannot configure s3 driver with out a provided bucket to use") self._check() self.prefix = self.config.get("prefix", "")
def _check_bucket(self): try: self.s3_client.get_bucket_location(Bucket=self.bucket_name) except Exception as ex: if type(ex).__name__ == 'NoSuchBucket' and self.create_bucket: self.s3_client.create_bucket(Bucket=self.bucket_name) else: logger.error( 'Error checking configured bucket for location during driver preflight check. Bucket = {}. Error = {}' .format(self.bucket_name, ex)) raise DriverConfigurationError(cause=ex)
def _check_container(self): try: resp = self.client.stat(container=self.container_name) except SwiftError as e: if e.exception.http_status == 404 and self.can_create_container: try: self.client.post(container=self.container_name) except Exception as e: logger.exception(e) raise e else: raise DriverConfigurationError(e)
def _check_bucket(self): try: self.s3_client.get_bucket_location(Bucket=self.bucket_name) except Exception as ex: if hasattr(ex, 'response') and ex.response.get('ResponseMetadata', {}).get('HTTPStatusCode') in [404]: if self.create_bucket: self.s3_client.create_bucket(Bucket=self.bucket_name) else: raise BucketNotFoundError(self.bucket_name) else: logger.error( 'Error checking configured bucket for location during driver preflight check. Bucket = {}. Error = {}'.format( self.bucket_name, ex)) raise DriverConfigurationError(cause=ex)