Python boto3.dynamodb.conditions.Key() Examples

The following are 30 code examples of boto3.dynamodb.conditions.Key(). 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 boto3.dynamodb.conditions , or try the search function .
Example #1
Source File: db.py    From autopush with Mozilla Public License 2.0 6 votes vote down vote up
def register_channel(self, uaid, channel_id, ttl=None):
        # type: (str, str, int) -> bool
        """Register a channel for a given uaid"""
        # Generate our update expression
        if ttl is None:
            ttl = self._max_ttl
        # Create/Append to list of channel_ids
        self.table.update_item(
            Key={
                'uaid': hasher(uaid),
                'chidmessageid': ' ',
            },
            UpdateExpression='ADD chids :channel_id SET expiry = :expiry',
            ExpressionAttributeValues={
                ":channel_id": set([normalize_id(channel_id)]),
                ":expiry": _expiry(ttl)
            },
        )
        return True 
Example #2
Source File: lambda_function.py    From aws-elemental-instant-video-highlights with Apache License 2.0 6 votes vote down vote up
def write_json():

    ## set tmp directory
    tmpdir = '/tmp/' + str(uuid.uuid4()) + '/'
    ensure_dir(tmpdir)

    rekog_type = 'minute'
    print("query of table: " + str(DYNAMO_MAIN))
    table = dynamodb.Table(DYNAMO_MAIN)    
    response = table.query(
        Limit=45,
        IndexName=DYNAMO_MAIN_GSI,        
        ScanIndexForward=False,
        KeyConditionExpression=Key('id_type').eq(rekog_type),
    )
    json_string = json.dumps(response['Items'], cls=DecimalEncoder)
    dynamo_filename = 'list-' + rekog_type + '.json'
    with open(tmpdir + dynamo_filename, 'w') as outfile:
        outfile.write(json_string)
    data = open(tmpdir + dynamo_filename, 'rb')
    pprint(s3.Bucket(S3_BUCKET).put_object(Key=dynamo_filename, Body=data))
    delete_file(tmpdir + dynamo_filename) 
Example #3
Source File: metadb.py    From boss with Apache License 2.0 6 votes vote down vote up
def delete_meta(self, lookup_key, key):
        """
        Delete the meta data item for the specified key
        Args:
            lookup_key: Key for the object requested
            key: Metadata key

        Returns:

        """

        response = self.table.delete_item(
            Key={
                'lookup_key': lookup_key,
                'key': key,
            },
            ReturnValues='ALL_OLD'
        )
        return response 
Example #4
Source File: metadb.py    From boss with Apache License 2.0 6 votes vote down vote up
def get_meta(self, lookup_key, key):
        """
        Retrieve the meta data for a given key
        Args:
            lookup_key: Key for the object requested
            key: Metadata key

        Returns:

        """

        response = self.table.get_item(
            Key={
                'lookup_key': lookup_key,
                'key': key,
            }
        )
        if 'Item' in response:
            return response['Item']
        else:
            return None 
Example #5
Source File: metadb.py    From boss with Apache License 2.0 6 votes vote down vote up
def write_meta(self, lookup_key, key, value):
        """
        Write the  meta data to dyanmodb
        Args:
            lookup_key: Key for the object requested
            key: Meta data key
            value: Metadata value

        Returns:

        """

        response = self.table.put_item(
            Item={
                'lookup_key': lookup_key,
                'key': key,
                'metavalue': value,
            }
        )
        return response 
Example #6
Source File: lambda_function.py    From aws-elemental-instant-video-highlights with Apache License 2.0 6 votes vote down vote up
def update_dyanmo_summary ( rekog_summary ):
    ## DynamoDB Update
    print("put_dynamo to table: " + str(DYNAMO_SUMMARY))
    table = dynamodb.Table(DYNAMO_SUMMARY)    # current
    response = table.update_item(
        Key={
            'rekog_label': rekog_summary['rekog_label'],
        },
        UpdateExpression="set timestamp_updated = :timestamp_updated, timestamp_ttl = :timestamp_ttl, rekog_type = :rekog_type, id_filename = :id_filename  ",
        ExpressionAttributeValues={
                ':timestamp_updated': rekog_summary['timestamp_updated'],
                ':timestamp_ttl': rekog_summary['timestamp_ttl'],
                ':rekog_type': rekog_summary['rekog_type'],
                ':id_filename': rekog_summary['id_filename'],
        },
        # ConditionExpression="job_state <> :completed",
        ReturnValues="UPDATED_NEW"
    )
    # print("dynamo update_item succeeded: {}".format(response))
    # pprint(response) 
Example #7
Source File: lambda_function.py    From aws-elemental-instant-video-highlights with Apache License 2.0 6 votes vote down vote up
def update_dyanmo_summary ( rekog_summary ):
    ## DynamoDB Update
    print("put_dynamo to table: " + str(DYNAMO_SUMMARY))
    table = dynamodb.Table(DYNAMO_SUMMARY)    # current
    response = table.update_item(
        Key={
            'rekog_label': rekog_summary['rekog_label'],
        },
        UpdateExpression="set timestamp_updated = :timestamp_updated, timestamp_ttl = :timestamp_ttl, rekog_type = :rekog_type, id_filename = :id_filename  ",
        ExpressionAttributeValues={
                ':timestamp_updated': rekog_summary['timestamp_updated'],
                ':timestamp_ttl': rekog_summary['timestamp_ttl'],
                ':rekog_type': rekog_summary['rekog_type'],
                ':id_filename': rekog_summary['id_filename'],
        },
        # ConditionExpression="job_state <> :completed",
        ReturnValues="UPDATED_NEW"
    )
    # print("dynamo update_item succeeded: {}".format(response))
    # pprint(response) 
Example #8
Source File: lambda_function.py    From aws-elemental-instant-video-highlights with Apache License 2.0 6 votes vote down vote up
def update_dyanmo_summary ( rekog_summary ):
    ## DynamoDB Update
    print("put_dynamo to table: " + str(DYNAMO_SUMMARY))
    table = dynamodb.Table(DYNAMO_SUMMARY)    # current
    response = table.update_item(
        Key={
            'rekog_label': rekog_summary['rekog_label'],
        },
        UpdateExpression="set timestamp_updated = :timestamp_updated, timestamp_ttl = :timestamp_ttl, rekog_type = :rekog_type, id_filename = :id_filename  ",
        ExpressionAttributeValues={
                ':timestamp_updated': rekog_summary['timestamp_updated'],
                ':timestamp_ttl': rekog_summary['timestamp_ttl'],
                ':rekog_type': rekog_summary['rekog_type'],
                ':id_filename': rekog_summary['id_filename'],
        },
        # ConditionExpression="job_state <> :completed",
        ReturnValues="UPDATED_NEW"
    )
    # print("dynamo update_item succeeded: {}".format(response))
    # pprint(response) 
Example #9
Source File: lambda_function.py    From aws-elemental-instant-video-highlights with Apache License 2.0 6 votes vote down vote up
def write_json():

    ## set tmp directory
    tmpdir = '/tmp/' + str(uuid.uuid4()) + '/'
    ensure_dir(tmpdir)

    print("query of table: " + str(DYNAMO_LIST))
    table = dynamodb.Table(DYNAMO_LIST)    
    response = table.query(
        Limit=15,
        ScanIndexForward=False,
        KeyConditionExpression=Key('label').eq('scoreboard'),
    )
    json_string = json.dumps(response['Items'], cls=DecimalEncoder)
    dynamo_filename = 'list-vod.json'
    with open(tmpdir + dynamo_filename, 'w') as outfile:
        outfile.write(json_string)

    ## S3 upload
    data = open(tmpdir + dynamo_filename, 'rb')
    print(s3.Bucket(S3_BUCKET).put_object(Key=dynamo_filename, Body=data))
    delete_file(tmpdir + dynamo_filename) 
Example #10
Source File: app.py    From aws-media-services-vod-automation with Apache License 2.0 6 votes vote down vote up
def job_progress(jobId):
    DYNAMO_TABLE_NAME = os.environ['JobTable']
    DYNAMO_INDEX_NAME = 'id-createdAt-index' 

    logger.info('GET progress for {} from {}'.format(jobId, DYNAMO_TABLE_NAME))
    
    try:
        table = dynamodb_resource.Table(DYNAMO_TABLE_NAME)
        
        response = table.query(IndexName=DYNAMO_INDEX_NAME, KeyConditionExpression=Key('id').eq(jobId))
    
    except ClientError as e:
        logger.info("ClientError from Dynamodb {}".format(e.response['Error']['Message']))
        raise BadRequestError("Dynamodb returned error message '%s'" % e.response['Error']['Message'])
    except Exception as e:
        logger.info("ClientError from Dynamodb {}".format(e.response['Error']['Message']))
        raise ChaliceViewError("Dynamodb returned error message '%s'" % e.response['Error']['Message'])

    print (json.dumps(response, cls=DecimalEncoder))
    # there is no information about this job yet
    if response['Count'] > 0:
        return response['Items'][0]
    else:
        return {} 
Example #11
Source File: lambda_function.py    From aws-elemental-instant-video-highlights with Apache License 2.0 6 votes vote down vote up
def update_dyanmo_summary ( rekog_summary ):
    ## DynamoDB Update
    print("put_dynamo to table: " + str(DYNAMO_SUMMARY))
    table = dynamodb.Table(DYNAMO_SUMMARY)    # current
    response = table.update_item(
        Key={
            'rekog_label': rekog_summary['rekog_label'],
        },
        UpdateExpression="set timestamp_updated = :timestamp_updated, timestamp_ttl = :timestamp_ttl, rekog_type = :rekog_type, id_filename = :id_filename  ",
        ExpressionAttributeValues={
                ':timestamp_updated': rekog_summary['timestamp_updated'],
                ':timestamp_ttl': rekog_summary['timestamp_ttl'],
                ':rekog_type': rekog_summary['rekog_type'],
                ':id_filename': rekog_summary['id_filename'],
        },
        # ConditionExpression="job_state <> :completed",
        ReturnValues="UPDATED_NEW"
    )
    print("dynamo update_item succeeded: {}".format(response))
    # pprint(response) 
Example #12
Source File: lambda_function.py    From aws-elemental-instant-video-highlights with Apache License 2.0 6 votes vote down vote up
def update_dyanmo_list ( rekog_summary ):
    ## DynamoDB Update
    print("put_dynamo to table: " + str(DYNAMO_LIST))
    table = dynamodb.Table(DYNAMO_LIST)    # current
    response = table.update_item(
        Key={
            'label': rekog_summary['label'],
            'label_sort': rekog_summary['label_sort'],
        },
        UpdateExpression="set emc_url = :emc_url, emc_status = :emc_status",
        ExpressionAttributeValues={
                ':emc_url': rekog_summary['emc_url'],
                ':emc_status': rekog_summary['emc_status'],
        },
        # ConditionExpression="job_state <> :completed",
        ReturnValues="UPDATED_NEW"
    )
    print("dynamo update_item succeeded: {}".format(response))
    # pprint(response) 
Example #13
Source File: metadb.py    From boss with Apache License 2.0 6 votes vote down vote up
def update_meta(self, lookup_key, key, new_value):
        """
        Update the Value for the given key
        Args:
            lookup_key: Key for the object requested
            key: Metadata key
            new_value: New meta data value

        Returns:

        """

        response = self.table.update_item(
            Key={
                'lookup_key': lookup_key,
                'key': key,
            },
            UpdateExpression='SET metavalue = :val1',
            ExpressionAttributeValues={
                ':val1': new_value
            },
            ReturnValues='UPDATED_NEW'
        )
        return response 
Example #14
Source File: lambda_function.py    From aws-elemental-instant-video-highlights with Apache License 2.0 6 votes vote down vote up
def update_dyanmo_main ( dynamo_object ):
    ## DynamoDB Update
    print("put_dynamo to table: " + str(DYNAMO_MAIN))
    print("dynamo_object: {}".format(dynamo_object))
    table = dynamodb.Table(DYNAMO_MAIN)
    response = table.update_item(
        Key={
            'id_filename': dynamo_object['id_filename'],
        },
        UpdateExpression="set timestamp_updated = :timestamp_updated, transcribe_status = :transcribe_status, transcribe_transcript = :transcribe_transcript  ",
        ExpressionAttributeValues={
                ':timestamp_updated': int((datetime.datetime.utcnow() - datetime.datetime(1970,1,1)).total_seconds()),
                ':transcribe_status': dynamo_object['transcribe_status'],
                ':transcribe_transcript': dynamo_object['transcribe_transcript'],
        },
        ReturnValues="UPDATED_NEW"
    )
    # print("dynamo update_item succeeded: {}".format(response))
    print("dynamo update_item succeeded")
    # pprint(response) 
Example #15
Source File: event_collector.py    From aws-media-services-vod-automation with Apache License 2.0 6 votes vote down vote up
def getMediaConvertJob(id, JOBTABLE):
    """
    Retrieve a job data structure from dynamodb
    :param id:  id of the job
    :param JOBTABLE: Name of the dynamodb job table for this stack
    """    
    try:
        table = DYNAMO_CLIENT.Table(JOBTABLE)
        response = table.get_item(Key={'id': id}, ConsistentRead=True)
    
    except ClientError as e:
        print(e.response['Error']['Message'])
    else:
        if 'Item' not in response:
            return None
        else:
            item = response['Item']
            print("GetItem succeeded:")
            #print(json.dumps(item, indent=4, cls=DecimalEncoder))
            return item 
Example #16
Source File: dynamodbstorage.py    From Flask-Blogging with MIT License 6 votes vote down vote up
def get_post_by_id(self, post_id):
        try:
            response = self._blog_posts_table.get_item(
                Key={'post_id': post_id}
            )
            item = response.get('Item')
            if item:
                r = item
                r['post_date'] = self._from_timestamp(r['post_date'])
                r['last_modified_date'] = \
                    self._from_timestamp(r['last_modified_date'])
                r["draft"] = bool(r["draft"])
            else:
                r = None
        except Exception as e:
            self._logger.exception(str(e))
            r = None
        return r 
Example #17
Source File: dynamodb.py    From boto3-examples with MIT License 6 votes vote down vote up
def delete_item(self, table_name, item_key):
        """
        delete an item
        PARAMS
        @table_name: name of the table
        @item_key: dict containing key and val of sort key
        e.g. {'name': 'uuid', 'value': 'some-uuid-val'}
        """
        dynamodb = self.conn
        table = dynamodb.Table(table_name)
        response = table.delete_item(
            Key={item_key['name']: item_key['value']}
        )
        if response['ResponseMetadata']['HTTPStatusCode'] == 200:
            return True
        else:
            return False 
Example #18
Source File: dynamodb.py    From boto3-examples with MIT License 6 votes vote down vote up
def update_item(self, table_name, key_dict, update_dict):
        """
        Update an item.
        PARAMS
        @table_name: name of the table
        @key_dict: dict containing the key name and val eg. {"uuid": item_uuid}
        @update_dict: dict containing the key name and val of
        attributes to be updated
        eg. {"attribute": "processing_status", "value": "completed"}
        """
        dynamodb = self.conn
        table = dynamodb.Table(table_name)
        update_expr = 'SET {} = :val1'.format(update_dict['attribute'])
        response = table.update_item(
            Key=key_dict,
            UpdateExpression=update_expr,
            ExpressionAttributeValues={
                ':val1': update_dict['value']
            }
        )
        if response['ResponseMetadata']['HTTPStatusCode'] == 200:
            return True
        else:
            return False 
Example #19
Source File: db.py    From autopush with Mozilla Public License 2.0 6 votes vote down vote up
def drop_user(self, uaid):
        # type: (str) -> bool
        """Drops a user record"""
        # The following hack ensures that only uaids that exist and are
        # deleted return true.
        try:
            item = self.table.get_item(
                Key={
                    'uaid': hasher(uaid)
                },
                ConsistentRead=True,
            )
            if 'Item' not in item:
                return False
        except ClientError:  # pragma nocover
            pass
        result = self.table.delete_item(
            Key={'uaid': hasher(uaid)})
        return result['ResponseMetadata']['HTTPStatusCode'] == 200 
Example #20
Source File: __init__.py    From aws-service-catalog-reference-architectures with Apache License 2.0 6 votes vote down vote up
def dynamo_query_multi(self, key, value_arr):
        '''
        Does a union of multiple queries for a single key and multiple values
        '''
        items = []
        for value in value_arr:
            resp = self._dynamo_table.query(
                ConsistentRead= True,
                KeyConditionExpression=Key(key).eq(value) 
            )   
            logger.debug(resp)
            items += resp["Items"]        
        return(items)

    #########################
    #   SSM Parameter
    ######################### 
Example #21
Source File: alert_table.py    From streamalert with Apache License 2.0 6 votes vote down vote up
def get_alert_record(self, rule_name, alert_id):
        """Get a single alert record from the alerts table.

        Args:
            rule_name (str): Name of the rule the alert triggered on
            alert_id (str): Alert UUID

        Returns:
            (dict): Dynamo record corresponding to this alert, or None if the alert was not found.
        """
        kwargs = {
            'ConsistentRead': True,
            'KeyConditionExpression': Key('RuleName').eq(rule_name) & Key('AlertID').eq(alert_id)
        }
        items = list(self._paginate(self._table.query, kwargs))
        return items[0] if items else {}

    # ---------- Add/Delete/Update Operations ---------- 
Example #22
Source File: alert_table.py    From streamalert with Apache License 2.0 6 votes vote down vote up
def mark_as_dispatched(self, alert):
        """Mark a specific alert as dispatched (in progress).

        Args:
            alert (Alert): Alert instance which has just been sent to the alert processor
        """
        # Update the alerts table with the dispatch time, but only if the alert still exists.
        # (The alert processor could have deleted the alert before this table update finishes).
        self._table.update_item(
            Key=alert.dynamo_key,
            UpdateExpression='SET Attempts = :attempts, Dispatched = :dispatched',
            ExpressionAttributeValues={
                ':attempts': alert.attempts,
                ':dispatched': alert.dispatched.strftime(Alert.DATETIME_FORMAT)
            },
            ConditionExpression='attribute_exists(AlertID)'
        ) 
Example #23
Source File: db.py    From autopush with Mozilla Public License 2.0 6 votes vote down vote up
def delete_message(self, notification):
        # type: (WebPushNotification) -> bool
        """Deletes a specific message"""
        if notification.update_id:
            try:
                self.table.delete_item(
                    Key={
                        'uaid': hasher(notification.uaid.hex),
                        'chidmessageid': notification.sort_key
                    },
                    Expected={
                        'updateid': {
                            'Exists': True,
                            'Value': notification.update_id
                            }
                    })
            except ClientError:
                return False
        else:
            self.table.delete_item(
                Key={
                    'uaid': hasher(notification.uaid.hex),
                    'chidmessageid': notification.sort_key,
                })
        return True 
Example #24
Source File: db.py    From autopush with Mozilla Public License 2.0 6 votes vote down vote up
def all_channels(self, uaid):
        # type: (str) -> Tuple[bool, Set[str]]
        """Retrieve a list of all channels for a given uaid"""

        # Note: This only returns the chids associated with the UAID.
        # Functions that call store_message() would be required to
        # update that list as well using register_channel()
        result = self.table.get_item(
            Key={
                'uaid': hasher(uuid.UUID(uaid).hex),
                'chidmessageid': ' ',
            },
            ConsistentRead=True
        )
        if result['ResponseMetadata']['HTTPStatusCode'] != 200:
            return False, set([])
        if 'Item' not in result:
            return False, set([])
        return True, result['Item'].get("chids", set([])) 
Example #25
Source File: support.py    From awslimits with Apache License 2.0 6 votes vote down vote up
def update_ticket(form):
    table = get_tickets_table()
    limit_type = form.limit_type.data
    table.update_item(
        Key={
            "display_id": form.display_id.data,
        },
        AttributeUpdates={
            'limit_type': {
                'Value': limit_type,
                'Action': 'PUT',
            },
            'limit_value': {
                'Value': form.limit_value.data,
                'Action': 'PUT',
            },
    })
    update_limit_value(limit_type) 
Example #26
Source File: db.py    From autopush with Mozilla Public License 2.0 6 votes vote down vote up
def unregister_channel(self, uaid, channel_id, **kwargs):
        # type: (str, str, **str) -> bool
        """Remove a channel registration for a given uaid"""
        chid = normalize_id(channel_id)

        response = self.table.update_item(
            Key={
                'uaid': hasher(uaid),
                'chidmessageid': ' ',
            },
            UpdateExpression="DELETE chids :channel_id SET expiry = :expiry",
            ExpressionAttributeValues={
                ":channel_id": set([chid]),
                ":expiry": _expiry(self._max_ttl)
            },
            ReturnValues="UPDATED_OLD",
        )
        chids = response.get('Attributes', {}).get('chids', {})
        if chids:
            try:
                return chid in chids
            except (TypeError, AttributeError):  # pragma: nocover
                pass
        # if, for some reason, there are no chids defined, return False.
        return False 
Example #27
Source File: dynamo_connection.py    From aws-syndicate with Apache License 2.0 6 votes vote down vote up
def get_item(self, table_name, hash_key_name, hash_key_value,
                 sort_key_name=None, sort_key_value=None):
        """ Get item from specified table by pk.

        :type table_name: str
        :type hash_key_name: str
        :param hash_key_value: str/number
        :type sort_key_name: str
        :param sort_key_value: str/number
        :returns list of items(if exists)
        """
        table = self.get_table_by_name(table_name)
        item = {hash_key_name: hash_key_value}
        if sort_key_name and sort_key_value:
            item[sort_key_name] = sort_key_value
        result = table.get_item(Key=item, ConsistentRead=True)
        if 'Item' in result:
            return result['Item'] 
Example #28
Source File: lambda_function.py    From aws-elemental-instant-video-highlights with Apache License 2.0 5 votes vote down vote up
def get_hops(rekog_label, id_filename, scan_index):
    table = dynamodb.Table(DYNAMO_MAIN)    
    exclusive_start_key = { 'id_type' : 'scenechange', 'id_filename': id_filename}
    response = table.query(
        IndexName=DYNAMO_MAIN_GSI,
        Limit=  1,
        ScanIndexForward=scan_index,
        ExclusiveStartKey=exclusive_start_key,
        KeyConditionExpression=Key('id_type').eq('scenechange'),
    )
    return response 
Example #29
Source File: dynamodb.py    From boto3-examples with MIT License 5 votes vote down vote up
def get_item(self, table_name, query_item):
        """
        Get an item given its key
        """
        dynamodb = self.conn
        table = dynamodb.Table(table_name)
        response = table.get_item(
            Key=query_item
        )
        item = response['Item']
        return item 
Example #30
Source File: db.py    From autopush with Mozilla Public License 2.0 5 votes vote down vote up
def get_uaid(self, uaid):
        # type: (str) -> Dict[str, Any]
        """Get the database record for the UAID

        :raises:
            :exc:`ItemNotFound` if there is no record for this UAID.
            :exc:`ProvisionedThroughputExceededException` if dynamodb table
            exceeds throughput.

        """
        try:
            item = self.table.get_item(
                Key={
                    'uaid': hasher(uaid)
                },
                ConsistentRead=True,
            )

            if item.get('ResponseMetadata').get('HTTPStatusCode') != 200:
                raise ItemNotFound('uaid not found')
            item = item.get('Item')
            if item is None:
                raise ItemNotFound("uaid not found")
            if item.keys() == ['uaid']:
                # Incomplete record, drop it.
                self.drop_user(uaid)
                raise ItemNotFound("uaid not found")
            # Mobile users do not check in after initial registration.
            # DO NOT EXPIRE THEM.
            return item
        except Boto3Error:  # pragma: nocover
            # We trap JSONResponseError because Moto returns text instead of
            # JSON when looking up values in empty tables. We re-throw the
            # correct ItemNotFound exception
            raise ItemNotFound("uaid not found")