Python botocore.exceptions.BotoCoreError() Examples

The following are 20 code examples of botocore.exceptions.BotoCoreError(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module botocore.exceptions , or try the search function .
Example #1
Source File: marketrecorder.py    From flumine with MIT License 6 votes vote down vote up
def _load(self, zip_file_dir: str, market_definition: dict) -> None:
        # note this will block the main handler queue during upload
        # todo create background worker instead?
        event_type_id = (
            market_definition.get("eventTypeId", 0) if market_definition else "7"
        )
        try:
            self.transfer.upload_file(
                filename=zip_file_dir,
                bucket=self._bucket,
                key=os.path.join(
                    self._data_type,
                    "streaming",
                    event_type_id,
                    os.path.basename(zip_file_dir),
                ),
                extra_args={
                    "Metadata": self._create_metadata(market_definition)
                    if market_definition
                    else {}
                },
            )
            logger.info("%s successfully loaded to s3" % zip_file_dir)
        except (BotoCoreError, Exception) as e:
            logger.error("Error loading to s3: %s" % e) 
Example #2
Source File: ec2.py    From cfn-sphere with Apache License 2.0 6 votes vote down vote up
def get_images(self, name_pattern):
        """
        Return list of AMIs matching the given name_pattern

        :param name_pattern: str: AMI name pattern
        :return: list(dict)
        :raise CfnSphereException:
        """
        filters = [
            {'Name': 'name', 'Values': [name_pattern]},
            {'Name': 'is-public', 'Values': ['false']},
            {'Name': 'state', 'Values': ['available']},
            {'Name': 'root-device-type', 'Values': ['ebs']}
        ]

        try:
            response = self.client.describe_images(ExecutableUsers=["self"], Filters=filters)
        except (BotoCoreError, ClientError) as e:
            raise CfnSphereBotoError(e)

        if not response['Images']:
            raise CfnSphereException("Could not find any private and available Taupage AMI")

        return response['Images'] 
Example #3
Source File: main.py    From binaryalert with Apache License 2.0 6 votes vote down vote up
def _process_md5(md5: str) -> bool:
    """Download the given file from CarbonBlack and upload to S3, returning True if successful."""
    try:
        binary = CARBON_BLACK.select(Binary, md5)
        _upload_to_s3(binary)
        return True
    except zipfile.BadZipFile:
        LOGGER.exception('[BadZipFile] Error downloading %s', md5)
        LOGGER.info('This md5 is invalid and will not retried')
        return False
    except (BotoCoreError, ServerError):
        LOGGER.exception('Error downloading %s', md5)
        LOGGER.error(
            'A temporary error was encountered during downloading. This md5 will be '
            'retried at a later time.'
        )
        raise
    except ObjectNotFoundError:
        LOGGER.exception('Error downloading %s', md5)
        LOGGER.info(
            'This may be caused due to a race condition where the requested binary is not yet '
            'available for download from the server. This binary will be retried at a later time.'
        )
        raise 
Example #4
Source File: cfn.py    From cfn-sphere with Apache License 2.0 6 votes vote down vote up
def create_stack(self, stack):
        self.logger.debug("Creating stack: {0}".format(stack))
        assert isinstance(stack, CloudFormationStack)

        try:
            stack_parameters_string = get_pretty_parameters_string(stack)

            self.logger.info(
                "Creating stack {0} ({1}) with parameters:\n{2}".format(stack.name,
                                                                        stack.template.name,
                                                                        stack_parameters_string))
            self._create_stack(stack)

            self.wait_for_stack_action_to_complete(stack.name, "create", stack.timeout)

            stack_outputs = get_pretty_stack_outputs(self.get_stack_outputs(stack))
            if stack_outputs:
                self.logger.info("Create completed for {0} with outputs: \n{1}".format(stack.name, stack_outputs))
            else:
                self.logger.info("Create completed for {0}".format(stack.name))
        except (BotoCoreError, ClientError, CfnSphereBotoError) as e:
            raise CfnStackActionFailedException("Could not create {0}: {1}".format(stack.name, e)) 
Example #5
Source File: base.py    From PynamoDB with MIT License 6 votes vote down vote up
def get_meta_table(self, table_name: str, refresh: bool = False):
        """
        Returns a MetaTable
        """
        if table_name not in self._tables or refresh:
            operation_kwargs = {
                TABLE_NAME: table_name
            }
            try:
                data = self.dispatch(DESCRIBE_TABLE, operation_kwargs)
                self._tables[table_name] = MetaTable(data.get(TABLE_KEY))
            except BotoCoreError as e:
                raise TableError("Unable to describe table: {}".format(e), e)
            except ClientError as e:
                if 'ResourceNotFound' in e.response['Error']['Code']:
                    raise TableDoesNotExist(e.response['Error']['Message'])
                else:
                    raise
        return self._tables[table_name] 
Example #6
Source File: cfn.py    From cfn-sphere with Apache License 2.0 6 votes vote down vote up
def delete_stack(self, stack):
        self.logger.debug("Deleting stack: {0}".format(stack))
        assert isinstance(stack, CloudFormationStack)

        try:
            self.logger.info("Deleting stack {0}".format(stack.name))
            self._delete_stack(stack)

            try:
                self.wait_for_stack_action_to_complete(stack.name, "delete", stack.timeout)
            except CfnSphereBotoError as e:
                if self.is_boto_stack_does_not_exist_exception(e.boto_exception):
                    pass
                else:
                    raise

            self.logger.info("Deletion completed for {0}".format(stack.name))
        except (BotoCoreError, ClientError, CfnSphereBotoError) as e:
            raise CfnStackActionFailedException("Could not delete {0}: {1}".format(stack.name, e)) 
Example #7
Source File: test_i_aws_encrytion_sdk_client.py    From aws-encryption-sdk-python with Apache License 2.0 5 votes vote down vote up
def test_remove_bad_client():
    test = KMSMasterKeyProvider()
    test.add_regional_client("us-fakey-12")

    with pytest.raises(BotoCoreError):
        test._regional_clients["us-fakey-12"].list_keys()

    assert not test._regional_clients 
Example #8
Source File: test_providers.py    From loafer with MIT License 5 votes vote down vote up
def test_fetch_messages_with_botocoreerror(mock_boto_session_sqs, boto_client_sqs):
    with mock_boto_session_sqs:
        error = BotoCoreError()
        boto_client_sqs.receive_message.side_effect = error

        provider = SQSProvider('queue-name')
        with pytest.raises(ProviderError):
            await provider.fetch_messages() 
Example #9
Source File: support_cases_aggregator.py    From aws-support-tickets-aggregator with MIT License 5 votes vote down vote up
def update_cases_helper(support_cases_table, account_id, client, kwargs):
    """

    :param support_cases_table: DDB table name
    :param account_id: account id
    :param client: DDB session object
    :param kwargs: pagination params
    :return: None
    """
    try:
        case_response = client.describe_cases(**kwargs)
    except (ClientError, BotoCoreError) as e:
        if e.response["Error"]["Code"] == "SubscriptionRequiredException":
            logging.error("Failed subscription for account %s; ignoring", account_id)
            return
        raise e
    for case in case_response.get("cases"):
        # WARNING: recentCommunications is only the last 5 communications.
        if case.get("recentCommunications", {}).get("nextToken"):
            del case["recentCommunications"]["nextToken"]

        # put updated info into table
        case["AccountId"] = account_id
        support_cases_table.put_item(Item=case)
    if "nextToken" in case_response:
        kwargs["nextToken"] = case_response["nextToken"]
        update_cases_helper(support_cases_table, account_id, client, kwargs) 
Example #10
Source File: support_cases_aggregator.py    From aws-support-tickets-aggregator with MIT License 5 votes vote down vote up
def list_account_ids():
    """
    Default requires permission to invoke organizations:ListAccounts API.

    DEFAULTS TO CALLING organizations:ListAccounts WITH CURRENT ROLE
    If CloudFormation stack is deployed in non-master AWS Organizations
    account, must assume role in that master AWS Organizations account.
    See README for details.
    """

    accounts = []
    assumed_role_arn = os.environ.get("ORG_MASTER_ACCOUNT_VIEWER_ROLE")
    if assumed_role_arn:
        session = get_session_with_arn(
            role_arn=assumed_role_arn, session_name="listAccountIds", base_session=None
        )
    else:
        session = boto3.session.Session()  # get local session
    try:
        client = session.client(
            "organizations", config=Config(retries={"max_attempts": 8})
        )
        paginator = client.get_paginator("list_accounts")
        response_iterator = paginator.paginate()
        for page in response_iterator:
            accounts.extend(page.get("Accounts", []))
    except (BotoCoreError, ClientError) as e:
        if e.response["Error"]["Code"] == "AccessDeniedException":
            logging.error(e)
            logging.error(
                "Could not call organizations:ListAccounts. "
                "Current account is likely not "
                "the AWS Organizations master account. "
                "See README for more details on setup. "
                "Returning empty list by default."
            )
        return []

    return [str(account_info.get("Id", "")) for account_info in accounts] 
Example #11
Source File: aws_common_utils_layer.py    From aws-support-tickets-aggregator with MIT License 5 votes vote down vote up
def get_session_with_arn(role_arn, session_name, base_session):
    """
    Returns a boto3.session.Session that assumed role_arn from base_session.

    base_session is the session used to assume the role;
        it must have permissions to assume the role.
    By default, base_session is the caller's regular boto3 session.
    """
    if not base_session:
        base_session = boto3.Session()

    if not session_name:
        session_name = "aws_common_utils"

    session_name = handle_session_name_length(session_name)
    client = base_session.client("sts")

    try:
        response = client.assume_role(RoleArn=role_arn, RoleSessionName=session_name)

        access_key = response["Credentials"]["AccessKeyId"]
        secret = response["Credentials"]["SecretAccessKey"]
        session_token = response["Credentials"]["SessionToken"]

        return boto3.Session(
            aws_access_key_id=access_key,
            aws_secret_access_key=secret,
            aws_session_token=session_token,
        )
    except (BotoCoreError, ClientError) as e:
        logging.error(
            "get_session_with_arn() failed trying to assume %s \
                       due to clienterror or botocore error",
            role_arn,
        )
        logging.error(str(e))
        raise e 
Example #12
Source File: cfn.py    From cfn-sphere with Apache License 2.0 5 votes vote down vote up
def validate_template(self, template):
        """
        Validate template
        :param template: CloudFormationTemplate
        :return: boolean (true if valid)
        """
        try:
            self.client.validate_template(TemplateBody=template.get_template_json())
            return True
        except (BotoCoreError, ClientError) as e:
            raise CfnSphereBotoError(e) 
Example #13
Source File: cfn.py    From cfn-sphere with Apache License 2.0 5 votes vote down vote up
def update_stack(self, stack):
        self.logger.debug("Updating stack: {0}".format(stack))
        assert isinstance(stack, CloudFormationStack)

        try:
            stack_parameters_string = get_pretty_parameters_string(stack)

            try:
                self._update_stack(stack)
            except ClientError as e:

                if self.is_boto_no_update_required_exception(e):
                    self.logger.info("Stack {0} does not need an update".format(stack.name))
                    return
                else:
                    self.logger.info(
                        "Updating stack {0} ({1}) with parameters:\n{2}".format(stack.name,
                                                                                stack.template.name,
                                                                                stack_parameters_string))
                    raise

            self.logger.info(
                "Updating stack {0} ({1}) with parameters:\n{2}".format(stack.name,
                                                                        stack.template.name,
                                                                        stack_parameters_string))

            self.wait_for_stack_action_to_complete(stack.name, "update", stack.timeout)

            stack_outputs = get_pretty_stack_outputs(self.get_stack_outputs(stack))
            if stack_outputs:
                self.logger.info("Update completed for {0} with outputs: \n{1}".format(stack.name, stack_outputs))
            else:
                self.logger.info("Update completed for {0}".format(stack.name))
        except (BotoCoreError, ClientError, CfnSphereBotoError) as e:
            raise CfnStackActionFailedException("Could not update {0}: {1}".format(stack.name, e)) 
Example #14
Source File: cfn.py    From cfn-sphere with Apache License 2.0 5 votes vote down vote up
def get_stack_events(self, stack_name):
        """
        Get recent stack events for a given stack_name
        :param stack_name: str
        :return: list(dict)
        """
        try:
            paginator = self.client.get_paginator('describe_stack_events')
            pages = paginator.paginate(StackName=stack_name, PaginationConfig={'MaxItems': 100})
            return next(iter(pages))["StackEvents"]
        except (BotoCoreError, ClientError) as e:
            raise CfnSphereBotoError(e) 
Example #15
Source File: cfn.py    From cfn-sphere with Apache License 2.0 5 votes vote down vote up
def get_stack_descriptions(self):
        """
        Get all stacks stack descriptions
        :return List(dict)
        :raise CfnSphereBotoError:
        """
        try:
            stacks = []
            for page in self.client.get_paginator('describe_stacks').paginate():
                stacks += page["Stacks"]
            return stacks
        except (BotoCoreError, ClientError) as e:
            raise CfnSphereBotoError(e) 
Example #16
Source File: cfn.py    From cfn-sphere with Apache License 2.0 5 votes vote down vote up
def get_stack_description(self, stack_name):
        """
        Get a stacks descriptions
        :param stack_name: string
        :return dict
        :raise CfnSphereBotoError:
        """
        try:
            return self.client.describe_stacks(StackName=stack_name)["Stacks"][0]
        except (BotoCoreError, ClientError) as e:
            raise CfnSphereBotoError(e) 
Example #17
Source File: cfn.py    From cfn-sphere with Apache License 2.0 5 votes vote down vote up
def get_stacks(self):
        """
        Get all stacks
        :return: List(boto3.resources.factory.cloudformation.Stack)
        :raise CfnSphereBotoError:
        """
        try:
            return list(self.resource.stacks.all())
        except (BotoCoreError, ClientError) as e:
            raise CfnSphereBotoError(e) 
Example #18
Source File: s3.py    From cfn-sphere with Apache License 2.0 5 votes vote down vote up
def get_contents_from_url(self, url):
        try:
            (_, bucket_name, key_name) = self._parse_url(url)
            s3_object = self.s3.Object(bucket_name, key_name)
            return s3_object.get(ResponseContentEncoding='utf-8')["Body"].read().decode('utf-8')
        except (Boto3Error, BotoCoreError, ClientError) as e:
            raise CfnSphereBotoError(e) 
Example #19
Source File: lambda_function.py    From amazon-polly-sample with Apache License 2.0 4 votes vote down vote up
def lambda_handler(event, context):
    rss = event['rss']
    bucket_name = event['bucket']
    logging.info("Processing url: %s" % rss)
    logging.info("Using bucket: %s" % bucket_name)

    session = Session(region_name="us-west-2")
    polly = session.client("polly")
    s3 = resource('s3')
    bucket = s3.Bucket(bucket_name)

    logging.info("getting list of existing objects in the given bucket")
    files = set(o.key for o in bucket.objects.all())

    feed = feedparser.parse(rss)

    title = feed['feed']['title']
    fg = FeedGenerator()
    fg.load_extension('podcast')
    fg.title('Audio podcast based on: %s' % title)
    fg.link(href=feed.feed.link, rel='alternate')
    fg.subtitle(feed.feed.description)

    ENTRY_URL = "http://{bucket}.s3-website.{region}.amazonaws.com/{filename}"

    for entry in get_entries(feed):
        filename = "%s.mp3" % entry['id']
        fe = fg.add_entry()
        fe.id(entry['id'])
        fe.title(entry['title'])
        fe.published(entry['published'])
        entry_url = ENTRY_URL.format(bucket=bucket_name, filename=filename, region=os.environ["AWS_REGION"])
        fe.enclosure(entry_url, 0, 'audio/mpeg')
        if filename in files:
            logging.info('Article "%s" with id %s already exist, skipping.'
                         % (entry['title'], entry['id']))
            continue
        try:
            logging.info("Next entry, size: %d" % len(entry['content']))
            logging.debug("Content: %s" % entry['content'])
            response = polly.synthesize_speech(
                    Text=entry['content'],
                    OutputFormat="mp3",
                    VoiceId="Joanna")
            with closing(response["AudioStream"]) as stream:
                bucket.put_object(Key=filename, Body=stream.read())
        except BotoCoreError as error:
            logging.error(error)
    bucket.put_object(Key='podcast.xml', Body=fg.rss_str(pretty=True)) 
Example #20
Source File: index.py    From aws-shell with Apache License 2.0 4 votes vote down vote up
def retrieve_candidate_values(self, service, operation, param):
        """Retrieve server side completions.

        :type service: str
        :param service: The service name, e.g. 'ec2', 'iam'.

        :type operation: str
        :param operation: The operation name, in the casing
            used by the CLI (words separated by hyphens), e.g.
            'describe-instances', 'delete-user'.

        :type param: str
        :param param: The param name, as specified in the service
            model, e.g. 'InstanceIds', 'UserName'.

        :rtype: list
        :return: A list of possible completions for the
            service/operation/param combination.  If no
            completions were found an empty list is returned.

        """
        # Example call:
        # service='ec2',
        # operation='terminate-instances',
        # param='InstanceIds'.
        if service not in self._describer_creator.services_with_completions():
            return []
        try:
            client = self._client_creator.create_client(service)
        except BotoCoreError as e:
            # create_client() could raise an exception if the session
            # isn't fully configured (say it's missing a region).
            # However, we don't want to turn off all server side
            # completions because it's still possible to create
            # clients for some services without a region, e.g. IAM.
            LOG.debug("Error when trying to create a client for %s",
                      service, exc_info=True)
            return []
        api_operation_name = client.meta.method_to_api_mapping.get(
            operation.replace('-', '_'))
        if api_operation_name is None:
            return []
        # Now we need to convert the param name to the
        # casing used by the API.
        completer = self._describer_creator.create_completer_query(service)
        result = completer.describe_autocomplete(
            service, api_operation_name, param)
        if result is None:
            return
        try:
            response = getattr(client, xform_name(result.operation, '_'))()
        except Exception as e:
            LOG.debug("Error when calling %s.%s: %s", service,
                      result.operation, e, exc_info=True)
            return
        results = jmespath.search(result.path, response)
        return results