def _process_post(location, transaction_type=''):
    """
    :param dict location:
    :param str transaction_type: Will be assigned to the 'trans_type' field in the exported file
    :return: tuple (response_data, response_status)
    """
    missing_keys = _missing_keys(location)
    if missing_keys:
        return {
            'error_message':
            'Missing keys: {0}'.format(', '.join(missing_keys))
        }, 400

    else:
        file_name = transaction_file_name(location)
        output_fd = BytesIO()
        tiername = application.config['TIERNAME']
        s3_bucket = application.config['S3_BUCKET']
        aws_region = application.config['AWS_REGION']
        destination_key = 'transactions/{0}/{1}'.format(tiername, file_name)
        write_transaction(output_fd,
                          location,
                          transaction_type=transaction_type)
        output_fd.seek(0)
        try:
            upload_to_s3(output_fd, destination_key, s3_bucket, aws_region)
        except (OSError, ValueError, ParamValidationError):
            return 'Unable to write the file', 500
        else:
            return 'File written to s3://{0}/{1}'.format(
                s3_bucket, destination_key), 200
Example #2
0
 def test_upload(self, mock_client):
     s3_connection_mock = Mock()
     s3_connection_mock.upload_fileobj.return_value = None
     mock_client.return_value = s3_connection_mock
     upload_to_s3(self.payload, self.destination_key, self.bucket,
                  self.region)
     mock_client.assert_called_with('s3', region_name=self.region)
     s3_connection_mock.upload_fileobj.assert_called_with(
         self.payload, Bucket=self.bucket, Key=self.destination_key)
Example #3
0
def _process_post(location, expected_model, transaction_type=''):
    """
    :param dict location:
    :param dict expected_model:
    :param str transaction_type: Will be assigned to the 'trans_type' field in the exported file
    :return: tuple (response_data, response_status)
    """
    expected_keys = set(iter(expected_model.keys()))

    missing_keys = _missing_keys(location, expected_keys)
    if missing_keys:
        return {
            'error_message':
            'Missing keys: {0}'.format(', '.join(missing_keys))
        }, 400

    else:
        file_name = transaction_file_name(location)
        output_fd = BytesIO()
        s3_bucket = application.config['S3_BUCKET']
        aws_region = application.config['AWS_REGION']
        endpoint = application.config['S3_ENDPOINT_URL']
        destination_key = 'transactions/{0}'.format(file_name)
        write_transaction(output_fd,
                          location,
                          transaction_type=transaction_type)
        output_fd.seek(0)
        try:
            upload_to_s3(output_fd, destination_key, s3_bucket, aws_region,
                         endpoint)
        except (OSError, ValueError, ParamValidationError) as err:
            application.logger.error(
                "An error occurred while attempting to upload the file to S3: "
                + str(err))
            return {'error_message': 'Unable to write the file to S3.'}, 500
        else:
            return 'File written to s3://{0}/{1}'.format(
                s3_bucket, destination_key), 200