Beispiel #1
0
def test_client_with_service(sts, cc):
    session = Session()
    chk = session.get_client('project-grand-slam')
    assert chk == 'boto!'
    cc.assert_called_with(ANY,
                          'project-grand-slam',
                          aws_access_key_id=None,
                          aws_secret_access_key=None,
                          aws_session_token=None,
                          region_name=None)
    sts.assert_not_called()
Beispiel #2
0
def test_get_client_with_role(sts, cc):

    sts.assume_role.return_value = 20
    sts().aws_access_key_id = 'my_access_key'
    sts().aws_secret_access_key = 'my_secret_key'
    sts().aws_session_token = 'my_session_token'

    test_role_arn = 'arn:aws:iam::111111111:role/net.dilex.some.test.role'
    session = Session(role_arn=test_role_arn)
    chk = session.get_client('project-grand-slam')

    assert chk == 'boto!'
    cc.assert_called_once()
    cc.assert_called_with(ANY,
                          'project-grand-slam',
                          aws_access_key_id='my_access_key',
                          aws_secret_access_key='my_secret_key',
                          aws_session_token='my_session_token',
                          region_name=None)
Beispiel #3
0
class S3():
    def __init__(self, client=None, region_name=None):
        self.bucket_name = None
        self.key_name = None
        self.session = Session(region_name=region_name)
        if client:
            self.s3 = client
        else:
            self.s3 = self.session.get_client('s3')


    def set_key_name(self, key_name):
        """set the key name property (i.e., the s3 'file' name)"""
        self.key_name = key_name


    def set_bucket_name(self, bucket_name):
        """sets the bucket_name property for class. CHecks that bucket exists"""
        try:
            response = self.s3.head_bucket(Bucket=bucket_name)
            print('Response:', response)
        except Exception as e:
            raise Exception("Bucket: " + bucket_name + " not available " + str(e))

        self.bucket_name = bucket_name
        return self.bucket_name


    def put_data_object(self, data):
        """store a data structure - like an array or dict"""
        enc_data = json.dumps(data)

        try:
            res = self.s3.put_object(
                Body=enc_data,
                Bucket=self.bucket_name,
                Key=self.key_name
            )
        except Exception as e:
            raise Exception('Error saving to S3: ' + str(e))

        return res


    def put_file_object(self, data):
        """store binary info - an image, encrypted data, word doc, etc"""

        try:
            res = self.s3.put_object(
                Body=data,
                Bucket=self.bucket_name,
                Key=self.key_name
            )
        except Exception as e:
            raise Exception('Error saving to S3: ' + str(e))

        return res


    def get_data_object(self):
        try:
            obj = self.s3.get_object(
                Bucket=self.bucket_name,
                Key=self.key_name
            )
        except Exception as e:
            raise Exception('Error reading from S3: ' + str(e))

        return json.loads(obj['Body'])


    def get_file_object(self):
        try:
            obj = self.s3.get_object(
                Bucket=self.bucket_name,
                Key=self.key_name
            )
        except Exception as e:
            raise Exception('Error reading from S3: ' + str(e))

        return obj['Body']


    def get_streaming_file_object(self):
        '''
            for reading objects from s3
            retutns a binary object which can be decoded
            with decode('utf-8') for example
        '''

        try:
            obj = self.s3.get_object(
                Bucket=self.bucket_name,
                Key=self.key_name
            )
        except Exception as e:
            raise Exception('Error reading from S3: ' + str(e))

        return obj['Body'].read()